Sub topic 16
Automating releases with GitHub Actions
This chapter assumes you have a pyproject.toml that builds cleanly with python -m build. It also assumes you have a GitHub repository and a PyPI account. It automates the manual build and twine upload steps so that publishing a GitHub release publishes the package.
What the workflow does
The workflow runs when you publish a GitHub release. It checks out the code, builds the sdist and wheel, and uploads them to PyPI. It authenticates with a short-lived OpenID Connect (OIDC) token. No API token is stored in the repository.
Configure the trusted publisher on PyPI
Before the workflow can upload, PyPI must trust it. In your project's publishing settings on PyPI, add a trusted publisher for GitHub with these values:
- Owner: your GitHub user or organization.
- Repository name: the repository that holds the workflow.
- Workflow filename: for example
publish.yml. - Environment name:
pypi.
For a project that does not exist on PyPI yet, create a pending publisher from your account's publishing page. The first successful upload then creates the project.
The workflow file
Save this as .github/workflows/publish.yml.
name: Publish
on:
release:
types: [published]
jobs:
publish:
runs-on: ubuntu-latest
environment: pypi
permissions:
id-token: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: python -m pip install build
- run: python -m build
- uses: pypa/gh-action-pypi-publish@release/v1The key parts are:
on: release: types: [published]runs the job only when a release is published, not on every push.permissions: id-token: writelets the job request an OIDC token. Without it, the publish step fails with a token error.environment: pypimust match the environment name you registered on PyPI. You can add required reviewers to this environment in the GitHub repository settings, so a person must approve each upload.pypa/gh-action-pypi-publishfinds the files indist/, exchanges the OIDC token for a temporary PyPI credential, and uploads.
Releasing
- Update
versioninpyproject.tomland commit it. - Create a tag such as
v0.1.1and a GitHub release from it. - Publish the release. The workflow starts and uploads the build.
PyPI rejects a filename it has already seen, so the version in pyproject.toml must be new for every release.
Common errors
| Symptom | Likely cause |
|---|---|
| Token request fails | `id-token: write` is missing from `permissions` |
| `invalid-publisher` error | Owner, repository, workflow filename, or environment differs from the PyPI settings |
| `File already exists` | The version was not bumped before the release |
| Workflow does not start | The release was saved as a draft, or the workflow file is not on the default branch |
Exercise
A release workflow runs but fails at the publish step with an error saying no trusted publisher matches. The workflow file is release.yml, but PyPI lists publish.yml. What do you change, and why?
Show answer
Make the two match, either by renaming the workflow file to publish.yml or by editing the trusted publisher on PyPI to say release.yml. PyPI compares the claims in the OIDC token, including the workflow filename, against its stored publisher settings, and it rejects the upload when any value differs.
Exercise
Why does the job need permissions: id-token: write when it already has a checkout step?
Show answer
Checkout reads repository contents and does not need that permission. The id-token: write permission is what allows the job to request an OIDC token from GitHub, which the publish action sends to PyPI in place of a stored API token.