|
| 1 | +name: Publish release to PyPI |
| 2 | + |
| 3 | +on: |
| 4 | + # Manual trigger – requires a tag input |
| 5 | + workflow_dispatch: |
| 6 | + inputs: |
| 7 | + tag: |
| 8 | + description: 'Git tag to publish (e.g. v1.8.0)' |
| 9 | + required: true |
| 10 | + # Automatic trigger when a draft becomes “published” |
| 11 | + release: |
| 12 | + types: [published] |
| 13 | + |
| 14 | +############################################################################### |
| 15 | +# ⬇️ Default permissions for the whole workflow: pull code + request OIDC |
| 16 | +############################################################################### |
| 17 | +permissions: |
| 18 | + contents: read # required by gh release download |
| 19 | + id-token: write # 🔑 enables token-less “Trusted Publisher” uploads |
| 20 | + |
| 21 | +jobs: |
| 22 | + publish-to-pypi: |
| 23 | + if: | |
| 24 | + (github.event_name == 'release' && github.event.release.prerelease == false) || |
| 25 | + (github.event_name == 'workflow_dispatch') |
| 26 | + runs-on: ubuntu-latest |
| 27 | + environment: pypi # GitHub Environment gate (reviewers etc.) |
| 28 | + |
| 29 | + steps: |
| 30 | + - uses: actions/checkout@v3 # makes refs/tags/* available for validation |
| 31 | + |
| 32 | + ######################################################################## |
| 33 | + # Resolve which tag to use and expose it via “outputs.tag” |
| 34 | + ######################################################################## |
| 35 | + - name: Determine tag |
| 36 | + id: tag |
| 37 | + run: | |
| 38 | + if [[ "${{ github.event_name }}" == "release" ]]; then |
| 39 | + echo "tag=${{ github.event.release.tag_name }}" >>"$GITHUB_OUTPUT" |
| 40 | + else |
| 41 | + echo "tag=${{ github.event.inputs.tag }}" >>"$GITHUB_OUTPUT" |
| 42 | + fi |
| 43 | +
|
| 44 | + ######################################################################## |
| 45 | + # Download the assets that were attached to that tag’s release |
| 46 | + ######################################################################## |
| 47 | + - name: Download release assets |
| 48 | + env: |
| 49 | + GH_TOKEN: ${{ github.token }} |
| 50 | + run: | |
| 51 | + gh release download \ |
| 52 | + "${{ steps.tag.outputs.tag }}" \ |
| 53 | + --repo "${{ github.repository }}" \ |
| 54 | + --dir dist |
| 55 | +
|
| 56 | + ######################################################################## |
| 57 | + # Publish everything in ./dist to PyPI via OIDC (no API token needed) |
| 58 | + ######################################################################## |
| 59 | + - name: Publish to PyPI |
| 60 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 61 | + # default packages_dir is "dist/" |
0 commit comments