Sub topic 16
Automating releases with GitHub Actions
Earlier chapters built, checked, and uploaded a release by hand. This chapter moves those steps into a GitHub Actions workflow. Publishing a GitHub release then triggers the build and the upload, and no long-lived PyPI token is stored anywhere.
This chapter assumes you have these things:
- A project that builds with
python -m build, as in the earlier chapters. - A GitHub repository holding that project.
- A PyPI account, and ideally a TestPyPI account.
- A basic idea of what a YAML workflow file is.
What the workflow has to do
Each release repeats the same sequence:
- Check out the tagged commit.
- Build the sdist and the wheel.
- Upload both files to PyPI.
Doing this in CI has three advantages. The build happens in a clean environment every time. The upload credential is never on a laptop. Every release is traceable to a commit and a workflow run.
The trigger is a published GitHub release. You create a tag such as v0.1.0, publish a release from it, and the workflow runs.
Step 1: Register a trusted publisher
The previous chapter covered trusted publishing. Here is the setup it needs. On PyPI, open your project, go to Publishing, and add a GitHub trusted publisher. You enter four values:
| Field | Example | Must match |
|---|---|---|
| Owner | `your-user` | The GitHub account or organization that owns the repository |
| Repository | `greeter` | The repository name |
| Workflow filename | `publish.yml` | The file name under `.github/workflows/`, not the workflow's `name:` |
| Environment | `pypi` | The `environment:` value in the job (optional, but recommended) |
If the project does not exist on PyPI yet, add a pending publisher from your account's publishing page instead. PyPI creates the project on the first successful upload.
Step 2: Write the workflow
The workflow from the spine is the minimal form. It builds and publishes in one job:
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/v1Each part does one job:
on.release.types: [published]runs the workflow when a release goes live. Drafts do not trigger it.environment: pypities the job to a GitHub environment. That environment can require a manual approval before the job runs.permissions: id-token: writelets the job request an OpenID Connect (OIDC) token from GitHub. PyPI exchanges that token for a short-lived upload credential.pypa/gh-action-pypi-publishuploads everything indist/. It needs nopasswordinput when trusted publishing is configured.
Step 3: Separate build from publish
The minimal version works, but every step in it, including pip install build, runs with permission to mint a publishing token. A safer pattern uses two jobs. The build job has no special permissions. The publish job holds id-token: write and does nothing except upload.
name: Publish
on:
release:
types: [published]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Check that the tag matches the project version
env:
TAG: ${{ github.event.release.tag_name }}
run: |
python - <<'EOF'
import os
import tomllib
with open("pyproject.toml", "rb") as f:
version = tomllib.load(f)["project"]["version"]
tag = os.environ["TAG"].removeprefix("v")
if version != tag:
raise SystemExit(f"Tag {tag} does not match version {version}")
EOF
- run: python -m pip install build twine
- run: python -m build
- run: python -m twine check dist/*
- uses: actions/upload-artifact@v4
with:
name: dist
path: dist/
publish:
needs: build
runs-on: ubuntu-latest
environment: pypi
permissions:
id-token: write
steps:
- uses: actions/download-artifact@v4
with:
name: dist
path: dist/
- uses: pypa/gh-action-pypi-publish@release/v1This version changes four things:
- The tag check fails the build early if you forgot to bump
versioninpyproject.toml. PyPI never allows a version to be uploaded twice, so catching this before the upload saves you a burned tag. - The tag name reaches the script through an environment variable. It is not pasted into the script text with
${{ }}. Tag names are user-controlled input, and interpolating them into shell code is a known injection route. twine checkvalidates the metadata, as you did by hand earlier.- The publish job never runs your build tooling. It only downloads the exact files the build job produced and uploads them.
Step 4: Add a TestPyPI rehearsal (optional)
You can rehearse in CI too. Register a second trusted publisher on TestPyPI, then add a job that points the action at TestPyPI's endpoint:
publish-testpypi:
needs: build
runs-on: ubuntu-latest
environment: testpypi
permissions:
id-token: write
steps:
- uses: actions/download-artifact@v4
with:
name: dist
path: dist/
- uses: pypa/gh-action-pypi-publish@release/v1
with:
repository-url: https://test.pypi.org/legacy/To keep the rehearsal ahead of the real release, make the publish job depend on it with needs: [build, publish-testpypi]. Be aware that TestPyPI also rejects reused versions. A failed real publish leaves you with a TestPyPI version you cannot rerun, so rehearse with the same version only once.
Step 5: Protect the release
Create the pypi environment under Settings → Environments in your repository. Two settings are worth turning on:
- Required reviewers. The publish job pauses until a named person approves it. This catches accidental releases.
- Deployment branches and tags. Restrict the environment to the release tags you expect, so a stray branch cannot publish.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `invalid-publisher` or token exchange failure | Owner, repository, workflow filename, or environment does not match the PyPI settings | Compare each field character by character |
| `Resource not accessible by integration` for the OIDC token | `id-token: write` is missing from the job that publishes | Add the permission to the publish job |
| `File already exists` | That version was already uploaded | Bump the version and make a new release. Do not reuse the tag. |
| Workflow does not start | The release was created as a draft, or a workflow using `GITHUB_TOKEN` created it | Publish the release manually, or create it with a personal token or GitHub App token |
| Tag check fails | `pyproject.toml` still has the old version | Bump the version, commit, and tag the new commit |
The fourth row catches many people. GitHub deliberately does not start new workflow runs for events created with the default GITHUB_TOKEN. A release-drafting workflow that uses that token therefore cannot trigger your publish workflow.
What attestations add
Recent versions of gh-action-pypi-publish generate digital attestations for your files by default when you publish with trusted publishing. An attestation is a signed statement that says which repository and workflow built the file. PyPI shows it on the file's page. You do not need to configure anything, but the feature only works under trusted publishing. This is another reason to prefer it over API tokens.
Exercise
Your publish job fails with "invalid-publisher". The workflow file is .github/workflows/release.yml, and its name: field is Publish. On PyPI you entered Publish as the workflow filename and pypi as the environment. What is wrong, and what do you change?
Show answer
PyPI expects the workflow file name, not the name: field. Change the trusted publisher's workflow filename on PyPI to release.yml. Then confirm the job has environment: pypi and that the owner and repository fields match the GitHub repository exactly.
Exercise
Why does the two-job workflow put id-token: write only on the publish job, and why does the tag-check script read the tag from an environment variable?
Show answer
Only the publish job needs to request an OIDC token. Keeping the permission off the build job means the build tools, which run third-party code, cannot request a publishing token. The tag name is user-controlled text, so passing it through an environment variable keeps it from being treated as shell code, which interpolating ${{ }} directly into the script could allow.