|
| 1 | +# Releasing is driven by __version__ in src/textbee/_version.py. |
| 2 | +# |
| 3 | +# Bump it, merge it to main, and this publishes it. The pull request carrying |
| 4 | +# the bump is the release proposal: reviewing and merging it is the approval |
| 5 | +# step, so there is no bot pull request on top of it. |
| 6 | +# |
| 7 | +# The guard compares the version against the git tags, not against the |
| 8 | +# previous commit, so the job is idempotent. Pushes that do not change the |
| 9 | +# version are a no-op, and a failed release can be retried by pushing again. |
| 10 | +# |
| 11 | +# Jobs are split by privilege. Only the publish job can mint a PyPI token, and |
| 12 | +# it runs nothing but the publish action. Actions are pinned to commit SHAs. |
| 13 | +name: Release |
| 14 | + |
| 15 | +on: |
| 16 | + push: |
| 17 | + branches: |
| 18 | + - main |
| 19 | + workflow_dispatch: |
| 20 | + |
| 21 | +permissions: |
| 22 | + contents: read |
| 23 | + |
| 24 | +# Never let two releases run at once, and never cancel one midway: an |
| 25 | +# interrupted run can leave a version on PyPI with no tag behind it. |
| 26 | +concurrency: |
| 27 | + group: release |
| 28 | + cancel-in-progress: false |
| 29 | + |
| 30 | +jobs: |
| 31 | + check: |
| 32 | + name: Check for an unreleased version |
| 33 | + # A dispatch from any other branch must never publish. |
| 34 | + if: github.ref == 'refs/heads/main' |
| 35 | + runs-on: ubuntu-latest |
| 36 | + timeout-minutes: 5 |
| 37 | + outputs: |
| 38 | + version: ${{ steps.check.outputs.version }} |
| 39 | + should_release: ${{ steps.check.outputs.should_release }} |
| 40 | + |
| 41 | + steps: |
| 42 | + - name: Checkout repository |
| 43 | + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 44 | + with: |
| 45 | + # Tags are the record of what has shipped, so fetch all of them. |
| 46 | + fetch-depth: 0 |
| 47 | + persist-credentials: false |
| 48 | + |
| 49 | + - name: Compare the package version against existing tags |
| 50 | + id: check |
| 51 | + run: | |
| 52 | + version="$(sed -n 's/^__version__ = "\(.*\)"$/\1/p' src/textbee/_version.py)" |
| 53 | + if [ -z "$version" ]; then |
| 54 | + echo "Could not read __version__ from src/textbee/_version.py" >&2 |
| 55 | + exit 1 |
| 56 | + fi |
| 57 | + echo "version=$version" >> "$GITHUB_OUTPUT" |
| 58 | +
|
| 59 | + if git rev-parse "v$version" >/dev/null 2>&1; then |
| 60 | + echo "should_release=false" >> "$GITHUB_OUTPUT" |
| 61 | + echo "v$version is already tagged, nothing to release." |
| 62 | + else |
| 63 | + echo "should_release=true" >> "$GITHUB_OUTPUT" |
| 64 | + echo "v$version has no tag yet, releasing." |
| 65 | + fi |
| 66 | +
|
| 67 | + build: |
| 68 | + name: Build v${{ needs.check.outputs.version }} |
| 69 | + needs: check |
| 70 | + if: needs.check.outputs.should_release == 'true' |
| 71 | + runs-on: ubuntu-latest |
| 72 | + timeout-minutes: 15 |
| 73 | + |
| 74 | + steps: |
| 75 | + - name: Checkout repository |
| 76 | + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 77 | + with: |
| 78 | + persist-credentials: false |
| 79 | + |
| 80 | + # No cache: a release builds from a clean slate. |
| 81 | + - name: Set up uv |
| 82 | + uses: astral-sh/setup-uv@07a047e608db84b3d8c06cd9a7c7dcc9c355c18e # v10.3.2 |
| 83 | + with: |
| 84 | + enable-cache: false |
| 85 | + |
| 86 | + - name: Install dependencies |
| 87 | + run: uv sync --locked |
| 88 | + |
| 89 | + - name: Lint |
| 90 | + run: uv run ruff check . |
| 91 | + |
| 92 | + - name: Typecheck |
| 93 | + run: uv run mypy |
| 94 | + |
| 95 | + - name: Unit test |
| 96 | + run: uv run pytest |
| 97 | + |
| 98 | + # The oldest supported Python, so a 3.9-only break cannot publish. |
| 99 | + - name: Unit test on Python 3.9 |
| 100 | + run: uv run --isolated --python 3.9 --group dev pytest |
| 101 | + |
| 102 | + - name: Build sdist and wheel |
| 103 | + run: uv build |
| 104 | + |
| 105 | + - name: Upload distributions |
| 106 | + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 |
| 107 | + with: |
| 108 | + name: dist |
| 109 | + path: dist/ |
| 110 | + if-no-files-found: error |
| 111 | + |
| 112 | + publish: |
| 113 | + name: Publish v${{ needs.check.outputs.version }} to PyPI |
| 114 | + needs: [check, build] |
| 115 | + runs-on: ubuntu-latest |
| 116 | + timeout-minutes: 10 |
| 117 | + # Must match the environment named in the PyPI trusted publisher. |
| 118 | + environment: |
| 119 | + name: pypi |
| 120 | + url: https://pypi.org/project/textbee/${{ needs.check.outputs.version }}/ |
| 121 | + permissions: |
| 122 | + # Minting the OIDC token PyPI exchanges for a publish credential. |
| 123 | + id-token: write |
| 124 | + |
| 125 | + steps: |
| 126 | + - name: Download distributions |
| 127 | + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 |
| 128 | + with: |
| 129 | + name: dist |
| 130 | + path: dist/ |
| 131 | + |
| 132 | + # PyPI publish runs before tagging on purpose. If it fails there is no |
| 133 | + # tag, so the next push retries cleanly. skip-existing lets that retry |
| 134 | + # move on to tagging when an earlier run already uploaded the files. |
| 135 | + # |
| 136 | + # No token: the OIDC exchange authenticates this publish, and the action |
| 137 | + # attaches PEP 740 attestations automatically. |
| 138 | + - name: Publish to PyPI |
| 139 | + uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3 |
| 140 | + with: |
| 141 | + skip-existing: true |
| 142 | + |
| 143 | + github-release: |
| 144 | + name: Tag and release v${{ needs.check.outputs.version }} |
| 145 | + needs: [check, publish] |
| 146 | + runs-on: ubuntu-latest |
| 147 | + timeout-minutes: 5 |
| 148 | + permissions: |
| 149 | + # Creating the tag and the GitHub release. |
| 150 | + contents: write |
| 151 | + |
| 152 | + steps: |
| 153 | + - name: Download distributions |
| 154 | + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 |
| 155 | + with: |
| 156 | + name: dist |
| 157 | + path: dist/ |
| 158 | + |
| 159 | + # Creates the tag as well as the release. Notes are generated from the |
| 160 | + # commits since the previous tag. |
| 161 | + - name: Create GitHub release |
| 162 | + env: |
| 163 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 164 | + GH_REPO: ${{ github.repository }} |
| 165 | + VERSION: ${{ needs.check.outputs.version }} |
| 166 | + run: | |
| 167 | + gh release create "v$VERSION" \ |
| 168 | + --title "v$VERSION" \ |
| 169 | + --target "$GITHUB_SHA" \ |
| 170 | + --generate-notes \ |
| 171 | + dist/* |
0 commit comments