|
| 1 | +name: release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - "v[0-9]+.[0-9]+.[0-9]+" |
| 7 | + - "v[0-9]+.[0-9]+.[0-9]+a[0-9]+" |
| 8 | + - "v[0-9]+.[0-9]+.[0-9]+b[0-9]+" |
| 9 | + - "v[0-9]+.[0-9]+.[0-9]+rc[0-9]+" |
| 10 | + |
| 11 | +env: |
| 12 | + PACKAGE_NAME: "lccs" |
| 13 | + MODULE_NAME: "lccs" |
| 14 | + OWNER: "brazil-data-cube" |
| 15 | + |
| 16 | +jobs: |
| 17 | + # Extract tag details |
| 18 | + details: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + outputs: |
| 21 | + new_version: ${{ steps.release.outputs.new_version }} |
| 22 | + suffix: ${{ steps.release.outputs.suffix }} |
| 23 | + tag_name: ${{ steps.release.outputs.tag_name }} |
| 24 | + clean_version: ${{ steps.clean_version.outputs.clean_version }} |
| 25 | + is_prerelease: ${{ steps.prerelease.outputs.is_prerelease }} |
| 26 | + |
| 27 | + steps: |
| 28 | + - uses: actions/checkout@v4 |
| 29 | + |
| 30 | + - name: Extract tag and Details |
| 31 | + id: release |
| 32 | + run: | |
| 33 | + if [ "${{ github.ref_type }}" = "tag" ]; then |
| 34 | + TAG_NAME=${GITHUB_REF#refs/tags/} |
| 35 | + NEW_VERSION=$(echo $TAG_NAME | awk -F'-' '{print $1}') |
| 36 | + SUFFIX=$(echo "$NEW_VERSION" | grep -oP '(a|b|rc)[0-9]+' | tr -d '\n' || true) |
| 37 | + echo "new_version=$NEW_VERSION" >> "$GITHUB_OUTPUT" |
| 38 | + echo "suffix=${SUFFIX:-}" >> "$GITHUB_OUTPUT" |
| 39 | + echo "tag_name=$TAG_NAME" >> "$GITHUB_OUTPUT" |
| 40 | + else |
| 41 | + echo "No tag found" |
| 42 | + exit 1 |
| 43 | + fi |
| 44 | +
|
| 45 | + - name: Extract clean version (remove 'v') |
| 46 | + id: clean_version |
| 47 | + run: | |
| 48 | + RAW="${{ steps.release.outputs.new_version }}" |
| 49 | + CLEAN="${RAW#v}" |
| 50 | + echo "clean_version=$CLEAN" >> $GITHUB_OUTPUT |
| 51 | +
|
| 52 | + - name: Detect prerelease |
| 53 | + id: prerelease |
| 54 | + run: | |
| 55 | + TAG="${{ steps.release.outputs.tag_name }}" |
| 56 | + if [[ "$TAG" =~ (a[0-9]+|b[0-9]+|rc[0-9]+)$ ]]; then |
| 57 | + echo "is_prerelease=true" >> $GITHUB_OUTPUT |
| 58 | + else |
| 59 | + echo "is_prerelease=false" >> $GITHUB_OUTPUT |
| 60 | + fi |
| 61 | +
|
| 62 | + # Detect branch and updated version |
| 63 | + detect-branch: |
| 64 | + needs: details |
| 65 | + runs-on: ubuntu-latest |
| 66 | + outputs: |
| 67 | + branch: ${{ steps.detect.outputs.branch }} |
| 68 | + version: ${{ steps.version.outputs.clean }} |
| 69 | + |
| 70 | + steps: |
| 71 | + - name: Checkout repository |
| 72 | + uses: actions/checkout@v4 |
| 73 | + with: |
| 74 | + fetch-depth: 0 |
| 75 | + |
| 76 | + - name: Detect branch for tag |
| 77 | + id: detect |
| 78 | + run: | |
| 79 | + BRANCH=$(git branch -r --contains "$GITHUB_SHA" | head -n 1 | sed 's/origin\///' | tr -d ' ') |
| 80 | + echo "branch=$BRANCH" >> $GITHUB_OUTPUT |
| 81 | +
|
| 82 | + - name: Extract version (remove leading v) |
| 83 | + id: version |
| 84 | + run: | |
| 85 | + CLEAN="${GITHUB_REF#refs/tags/v}" |
| 86 | + echo "clean=$CLEAN" >> $GITHUB_OUTPUT |
| 87 | +
|
| 88 | + bump-version: |
| 89 | + needs: detect-branch |
| 90 | + runs-on: ubuntu-latest |
| 91 | + permissions: |
| 92 | + contents: write |
| 93 | + env: |
| 94 | + BRANCH: ${{ needs.detect-branch.outputs.branch }} |
| 95 | + VERSION: ${{ needs.detect-branch.outputs.version }} |
| 96 | + |
| 97 | + steps: |
| 98 | + - name: Checkout correct branch |
| 99 | + uses: actions/checkout@v4 |
| 100 | + with: |
| 101 | + fetch-depth: 0 |
| 102 | + ref: ${{ env.BRANCH }} |
| 103 | + |
| 104 | + - name: Update pyproject.toml version |
| 105 | + run: | |
| 106 | + sed -i "s/^version = .*/version = \"${VERSION}\"/" pyproject.toml |
| 107 | +
|
| 108 | + - name: Validate pyproject.toml |
| 109 | + run: | |
| 110 | + pip install validate-pyproject |
| 111 | + validate-pyproject pyproject.toml |
| 112 | +
|
| 113 | + - name: Commit version bump |
| 114 | + run: | |
| 115 | + git config user.name "github-actions" |
| 116 | + git config user.email "[email protected]" |
| 117 | + git add pyproject.toml |
| 118 | + git commit -m "Release ${VERSION}: update version" |
| 119 | + git push origin HEAD:${BRANCH} |
| 120 | +
|
| 121 | + - name: Upload updated pyproject |
| 122 | + uses: actions/upload-artifact@v4 |
| 123 | + with: |
| 124 | + name: updated-project |
| 125 | + path: | |
| 126 | + pyproject.toml |
| 127 | +
|
| 128 | + # Check version on Pypi |
| 129 | + check_pypi: |
| 130 | + needs: details |
| 131 | + runs-on: ubuntu-latest |
| 132 | + |
| 133 | + steps: |
| 134 | + - name: Fetch information from PyPI |
| 135 | + run: | |
| 136 | + response=$(curl -s https://pypi.org/pypi/${{ env.PACKAGE_NAME }}/json || echo "{}") |
| 137 | + latest_previous_version=$(echo $response | grep -oP '"releases":\{"\K[^"]+' | sort -rV | head -n 1) |
| 138 | + if [ -z "$latest_previous_version" ]; then |
| 139 | + echo "Package not found on PyPI." |
| 140 | + latest_previous_version="0.0.0" |
| 141 | + fi |
| 142 | + echo "latest_previous_version=$latest_previous_version" >> $GITHUB_ENV |
| 143 | +
|
| 144 | + - name: Compare versions and exit if not newer |
| 145 | + run: | |
| 146 | + NEW_VERSION=${{ needs.details.outputs.new_version }} |
| 147 | + LATEST_VERSION=$latest_previous_version |
| 148 | + if [ "$(printf '%s\n' "$LATEST_VERSION" "$NEW_VERSION" | sort -rV | head -n 1)" != "$NEW_VERSION" ] \ |
| 149 | + || [ "$NEW_VERSION" == "$LATEST_VERSION" ]; then |
| 150 | + echo "The new version $NEW_VERSION is not greater than $LATEST_VERSION" |
| 151 | + exit 1 |
| 152 | + fi |
| 153 | +
|
| 154 | + # Build Package |
| 155 | + setup_and_build: |
| 156 | + needs: [details, check_pypi, bump-version] |
| 157 | + runs-on: ubuntu-latest |
| 158 | + |
| 159 | + steps: |
| 160 | + - uses: actions/checkout@v4 |
| 161 | + |
| 162 | + - uses: actions/download-artifact@v4 |
| 163 | + with: |
| 164 | + name: updated-project |
| 165 | + path: . |
| 166 | + |
| 167 | + - name: Set up Python |
| 168 | + uses: actions/setup-python@v5 |
| 169 | + with: |
| 170 | + python-version: "3.12" |
| 171 | + |
| 172 | + - name: Install build and twine |
| 173 | + run: | |
| 174 | + python3 -m pip install --upgrade pip |
| 175 | + python3 -m pip install build twine |
| 176 | +
|
| 177 | + - name: Generate stubs |
| 178 | + run: | |
| 179 | + python3 -m pip install mypy |
| 180 | + stubgen ${{ env.MODULE_NAME }} -o . |
| 181 | +
|
| 182 | + - name: Build source and wheel distribution |
| 183 | + run: | |
| 184 | + python3 -m build |
| 185 | +
|
| 186 | + - name: Upload artifacts |
| 187 | + uses: actions/upload-artifact@v4 |
| 188 | + with: |
| 189 | + name: dist |
| 190 | + path: dist/ |
| 191 | + |
| 192 | + # Publish in TestPyPI |
| 193 | + testpypi_publish: |
| 194 | + name: Upload to TestPyPI (SAFE TEST) |
| 195 | + needs: [setup_and_build, details] |
| 196 | + runs-on: ubuntu-latest |
| 197 | + |
| 198 | + steps: |
| 199 | + - name: Download artifacts |
| 200 | + uses: actions/download-artifact@v4 |
| 201 | + with: |
| 202 | + name: dist |
| 203 | + path: dist/ |
| 204 | + |
| 205 | + - name: Upload to TestPyPI using twine |
| 206 | + env: |
| 207 | + TWINE_USERNAME: "__token__" |
| 208 | + TWINE_PASSWORD: ${{ secrets.TEST_PYPI_API_TOKEN }} |
| 209 | + run: | |
| 210 | + python3 -m pip install twine |
| 211 | + python3 -m twine upload --repository-url https://test.pypi.org/legacy/ dist/* |
| 212 | +
|
| 213 | + # Publish to PYPI |
| 214 | + pypi_publish: |
| 215 | + name: Upload release to PyPI |
| 216 | + needs: [testpypi_publish, setup_and_build, details] |
| 217 | + if: ${{ needs.details.outputs.is_prerelease == 'false' }} |
| 218 | + runs-on: ubuntu-latest |
| 219 | + environment: |
| 220 | + name: release |
| 221 | + permissions: |
| 222 | + id-token: write |
| 223 | + |
| 224 | + steps: |
| 225 | + - name: Download artifacts |
| 226 | + uses: actions/download-artifact@v4 |
| 227 | + with: |
| 228 | + name: dist |
| 229 | + path: dist/ |
| 230 | + |
| 231 | + - name: Publish distribution to PyPI |
| 232 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 233 | + |
| 234 | + # Pre-Release and Release jobs |
| 235 | + github_prerelease: |
| 236 | + name: Create GitHub Pre-Release |
| 237 | + runs-on: ubuntu-latest |
| 238 | + needs: |
| 239 | + - details |
| 240 | + - setup_and_build |
| 241 | + - testpypi_publish |
| 242 | + |
| 243 | + if: > |
| 244 | + needs.testpypi_publish.result == 'success' && |
| 245 | + needs.details.outputs.is_prerelease == 'true' |
| 246 | +
|
| 247 | + permissions: |
| 248 | + contents: write |
| 249 | + |
| 250 | + steps: |
| 251 | + - name: Checkout code |
| 252 | + uses: actions/checkout@v4 |
| 253 | + with: |
| 254 | + fetch-depth: 0 |
| 255 | + |
| 256 | + - name: Create GitHub Pre-Release |
| 257 | + uses: softprops/action-gh-release@v2 |
| 258 | + with: |
| 259 | + tag_name: ${{ needs.details.outputs.tag_name }} |
| 260 | + name: "Version ${{ needs.details.outputs.clean_version }}" |
| 261 | + generate_release_notes: true |
| 262 | + prerelease: true |
| 263 | + env: |
| 264 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 265 | + |
| 266 | + github_release: |
| 267 | + name: Create GitHub Release |
| 268 | + runs-on: ubuntu-latest |
| 269 | + needs: |
| 270 | + - details |
| 271 | + - setup_and_build |
| 272 | + - testpypi_publish |
| 273 | + - pypi_publish |
| 274 | + |
| 275 | + if: > |
| 276 | + needs.testpypi_publish.result == 'success' && |
| 277 | + needs.pypi_publish.result == 'success' && |
| 278 | + needs.details.outputs.is_prerelease == 'false' |
| 279 | +
|
| 280 | + permissions: |
| 281 | + contents: write |
| 282 | + |
| 283 | + steps: |
| 284 | + - name: Checkout code |
| 285 | + uses: actions/checkout@v4 |
| 286 | + with: |
| 287 | + fetch-depth: 0 |
| 288 | + |
| 289 | + - name: Create GitHub Release |
| 290 | + uses: softprops/action-gh-release@v2 |
| 291 | + with: |
| 292 | + tag_name: ${{ needs.details.outputs.tag_name }} |
| 293 | + name: "Version ${{ needs.details.outputs.clean_version }}" |
| 294 | + generate_release_notes: true |
| 295 | + prerelease: false |
| 296 | + env: |
| 297 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments