comlink-python release #48
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: comlink-python release | |
| # Tag-driven release. | |
| # | |
| # The package version is derived from the Git tag at build time via hatch-vcs | |
| # (see [tool.hatch.version] in pyproject.toml), so a release does NOT commit a | |
| # version bump to the repository. This keeps the workflow compatible with the | |
| # protected `main` branch (which forbids direct pushes). | |
| # | |
| # Ordering is build -> publish -> tag so the Git tag / GitHub Release is created | |
| # only after a successful PyPI publish, which avoids leaving an orphaned tag | |
| # behind if the build or upload fails. | |
| on: | |
| workflow_dispatch: {} | |
| permissions: | |
| contents: read | |
| jobs: | |
| build: | |
| name: Build distributions | |
| runs-on: ubuntu-latest | |
| if: github.ref == 'refs/heads/main' | |
| concurrency: | |
| group: ${{ github.workflow }}-release-${{ github.ref_name }} | |
| cancel-in-progress: false | |
| outputs: | |
| version: ${{ steps.version.outputs.version }} | |
| tag: ${{ steps.version.outputs.tag }} | |
| sha: ${{ steps.version.outputs.sha }} | |
| steps: | |
| - name: Check-out repository | |
| uses: actions/checkout@v7 | |
| with: | |
| ref: ${{ github.ref_name }} | |
| fetch-depth: 0 # full history + tags for version derivation | |
| - name: Install uv | |
| uses: astral-sh/setup-uv@v7 | |
| - name: Set up Python | |
| uses: actions/setup-python@v7 | |
| with: | |
| python-version: "3.12" | |
| - name: Read release version from CHANGELOG.md | |
| id: version | |
| run: | | |
| set -euo pipefail | |
| version="$(grep -m1 -oE '^## \[v?[0-9]+\.[0-9]+\.[0-9]+[^]]*\]' CHANGELOG.md \ | |
| | sed -E 's/^## \[(v?[0-9][^]]*)\]$/\1/')" | |
| if [ -z "${version}" ]; then | |
| echo "::error::Could not read a version heading from CHANGELOG.md." | |
| exit 1 | |
| fi | |
| tag="v${version#v}" | |
| semver="${tag#v}" | |
| if git rev-parse "${tag}" >/dev/null 2>&1; then | |
| echo "::error::Tag ${tag} already exists — was CHANGELOG.md updated for a new version?" | |
| exit 1 | |
| fi | |
| { | |
| echo "version=${semver}" | |
| echo "tag=${tag}" | |
| echo "sha=${GITHUB_SHA}" | |
| } >> "$GITHUB_OUTPUT" | |
| echo "Releasing ${tag} (from CHANGELOG.md) at ${GITHUB_SHA}" | |
| - name: Build the package | |
| env: | |
| # Force the build version without needing the tag to exist yet, so the | |
| # tag can be created last (after a successful publish). | |
| SETUPTOOLS_SCM_PRETEND_VERSION: ${{ steps.version.outputs.version }} | |
| run: uvx --with hatch-vcs hatch build | |
| - name: Verify built version matches the target | |
| run: | | |
| set -euo pipefail | |
| ls -1 dist/ | |
| test -f "dist/swgoh_comlink-${{ steps.version.outputs.version }}.tar.gz" | |
| test -f "dist/swgoh_comlink-${{ steps.version.outputs.version }}-py3-none-any.whl" | |
| - name: Upload build artifacts | |
| uses: actions/upload-artifact@v7 | |
| with: | |
| name: dist | |
| path: dist/ | |
| pypi-publish: | |
| name: Publish to PyPI | |
| runs-on: ubuntu-latest | |
| needs: | |
| - build | |
| permissions: | |
| # IMPORTANT: this permission is mandatory for trusted publishing | |
| id-token: write | |
| environment: | |
| name: pypi | |
| url: https://pypi.org/project/swgoh-comlink/ | |
| steps: | |
| - name: Retrieve release distributions | |
| uses: actions/download-artifact@v8 | |
| with: | |
| name: dist | |
| path: dist/ | |
| - name: Publish release distributions to PyPI | |
| uses: pypa/gh-action-pypi-publish@dc37677b2e1c63e2034f94d8a5b11f265b73ba33 # v1.14.2 | |
| tag-release: | |
| name: Tag and create GitHub Release | |
| runs-on: ubuntu-latest | |
| needs: | |
| - build | |
| - pypi-publish | |
| permissions: | |
| contents: write # create the tag + GitHub Release | |
| steps: | |
| - name: Create tag and GitHub Release | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| set -euo pipefail | |
| gh release create "${{ needs.build.outputs.tag }}" \ | |
| --repo "${{ github.repository }}" \ | |
| --target "${{ needs.build.outputs.sha }}" \ | |
| --title "${{ needs.build.outputs.tag }}" \ | |
| --generate-notes |