Sub topic 15
Trusted publishing: releasing without long-lived tokens
This chapter assumes you can already build and upload a release by hand with python -m build and twine. It also assumes you have a PyPI account, a GitHub repository, and basic familiarity with GitHub Actions workflow files.
Why long-lived tokens are a liability
The usual way to automate an upload is to create a PyPI API token, paste it into a GitHub secret, and pass it to twine. That works, but the token has these properties:
- It stays valid until someone revokes it.
- It can upload to every project it is scoped to, from anywhere.
- It sits in a secret store, and from there it can end up in logs, forks, backups, or a compromised dependency's build step.
- Someone has to rotate it, and in practice rotation often doesn't happen.
Trusted publishing replaces the stored secret with a statement of trust. You tell PyPI: "Uploads for this project may come from this workflow in this repository." The CI system then proves its identity at run time, and PyPI hands back a token that expires in minutes. Nothing durable exists to steal.
How the exchange works
Trusted publishing is built on OpenID Connect (OIDC). OIDC is a standard in which an identity provider signs a short document, called an ID token, that states who is asking. For GitHub Actions, the provider is GitHub, and the token contains claims such as the repository, the workflow file, and the environment.
Three details matter in practice:
- The signature proves the claims come from GitHub. PyPI doesn't trust anything the workflow says about itself.
- PyPI compares the claims with the publisher you registered. If any registered field differs, the exchange fails.
- The API token PyPI returns is scoped to your project and expires after a short time, on the order of minutes. It is only useful during that job.
Registering a trusted publisher on PyPI
You configure trust on PyPI, in the project's settings under the publishing section. You add a GitHub publisher with these fields:
| Field | What to enter | Notes |
|---|---|---|
| Owner | The GitHub user or organization | Case matters less than spelling. A typo fails silently until upload time. |
| Repository name | The repository, without the owner | For example `greeter`. |
| Workflow filename | The file name only, such as `publish.yml` | It must match the file in `.github/workflows/`. |
| Environment name | The GitHub environment, such as `pypi` | Optional on PyPI's side, but recommended. |
If the project doesn't exist on PyPI yet, use a pending publisher. You add it from your account's publishing page, and the first successful upload creates the project and converts the pending entry into a normal trusted publisher. Until then, nobody else can claim the name through this route.
TestPyPI is a separate service with separate accounts and settings. To rehearse with trusted publishing, register the same publisher there, preferably with a different environment name such as testpypi.
The workflow
The chapter on release automation shows a compact workflow. Here is the same idea with the build and the upload split into separate jobs, which limits where the identity permission exists.
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"
- run: python -m pip install build
- run: python -m build
- 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/v1Points to check against what you registered:
- The file is saved as
.github/workflows/publish.yml, so the workflow filename on PyPI ispublish.yml. - The
publishjob declaresenvironment: pypi, so the environment name on PyPI ispypi. permissions: id-token: writelets the job request an ID token. It grants no write access to your repository contents.- The publish job never checks out your code or runs
pip install. It uploads files built earlier, so fewer steps can tamper with them or read the token.
The publish action detects that no password was supplied, requests the ID token, exchanges it with PyPI, and uploads everything in dist/. You don't need a with: password: line or a repository secret.
Using an environment as a gate
A GitHub environment is a named deployment target in your repository settings. Naming one in the PyPI publisher adds a condition the claims must satisfy. Combined with environment protection rules, it gives you a manual gate:
- Create an environment called
pypiin the repository settings. - Add required reviewers.
- When a release is published, the
publishjob pauses until a reviewer approves it.
This means a stray workflow edit or an unexpected trigger can't upload a release without a person agreeing to it. You can also restrict which branches or tags are allowed to deploy to that environment.
Attestations
The publish action also generates attestations by default when it uses trusted publishing. An attestation is a signed statement that links an uploaded file to the repository and workflow that built it. PyPI stores it next to the file, so consumers can verify provenance. You don't have to do anything for this, but it only works from a trusted publisher setup, not from a stored token.
Common failures
| Symptom | Likely cause | Fix |
|---|---|---|
| Error saying the token was valid but no matching publisher exists | One registered field differs from the claims. Usual suspects are the workflow filename, a renamed repository, or the environment name. | Compare the claims printed in the error with the PyPI settings and correct the mismatch. |
| The action can't request an ID token | The job lacks `id-token: write`, or the workflow runs from a fork pull request | Add the permission to the publish job. Fork pull requests never receive OIDC tokens. |
| Works on TestPyPI, fails on PyPI | The publisher was registered on only one service | Register it on both, each with its own environment name. |
| Upload rejected because the version exists | You re-ran a release for an already-published version | Bump the version. PyPI never lets you overwrite a file. |
| Publisher stops matching after a refactor | The workflow was renamed or moved, or the repository was transferred or renamed | Update the publisher on PyPI to the new values. |
If you call the publishing workflow from another workflow, the claims still refer to the calling workflow, not the reusable one. Register the file that is the entry point of the run.
Removing the old token
After one successful trusted release, delete the old API token from PyPI and the secret from GitHub. A leftover token defeats the purpose, because it is the credential an attacker would still be able to use. If you require two-factor authentication on the PyPI account, as PyPI itself already requires for uploads, trusted publishing doesn't change that. It affects only how CI authenticates.
Exercise
Your publish job fails with an error saying the identity token is valid but no trusted publisher matches. The workflow is .github/workflows/release.yml, the job uses environment: pypi, and the PyPI publisher was registered with workflow filename publish.yml and environment pypi. What do you change, and why is no secret involved in the fix?
Show answer
Either rename the workflow file to publish.yml or edit the publisher on PyPI to say release.yml. PyPI compares the workflow filename in the signed GitHub claims with the registered value, and release.yml does not equal publish.yml. No secret is involved because the credential is the signed identity claims, not a stored token. The fix only makes the registered trust settings match the real workflow.
Exercise
Why does the example split the build and the publish steps into two jobs, and which job should hold id-token: write?
Show answer
Only the publish job should hold id-token: write. The build job runs pip install and your build backend, which execute third-party code. If that code could request an identity token, it could mint an upload token. Keeping the permission in a job that only downloads and uploads the finished files limits that risk.