Sub topic 15
Trusted publishing: releasing without long-lived tokens
This chapter assumes you can build and upload a package with twine, and that your project lives in a GitHub repository. You should know what an API token is and why leaking one is a problem.
The problem with API tokens
The usual way to upload to PyPI is an API token. The token is a long-lived secret. You paste it into a CI settings page or a ~/.pypirc file, and anyone who gets a copy can publish releases under your project's name until you revoke it.
Tokens leak through logs, copied config files, compromised laptops, and over-broad CI permissions. They also tend to be forgotten, so they are rarely rotated.
How trusted publishing works
Trusted publishing replaces the stored secret with a trust relationship. You tell PyPI which CI workflow is allowed to publish your project. When that workflow runs, it asks its CI provider for a signed identity document, which is an OpenID Connect (OIDC) token. PyPI checks the signature and the claims in it. If they match your configuration, PyPI issues an upload token that expires after a few minutes.
Nothing secret is stored in the repository. A stolen copy of the workflow file is useless outside your repository.
Configuring the trusted publisher
On PyPI, open your project, choose Publishing, and add a GitHub publisher. You enter four values:
| Field | Example | Meaning |
|---|---|---|
| Owner | `your-account` | The GitHub user or organization |
| Repository | `greeter` | The repository that runs the workflow |
| Workflow name | `publish.yml` | The file under `.github/workflows/` |
| Environment | `pypi` | Optional, but recommended |
PyPI compares these values with the claims in the OIDC token. If any value differs, the upload is rejected.
If the project does not exist on PyPI yet, add a pending publisher from your account's publishing page. The first successful upload then creates the project.
What the workflow must provide
Look at the workflow from the previous chapter. Two lines make trusted publishing work:
environment: pypi
permissions:
id-token: writeid-token: writelets the job request an OIDC token. Without it, the publish step fails with a missing-token error.environment: pypimust match the environment name registered on PyPI. In GitHub you can add required reviewers to this environment, so a release waits for a human approval.
The pypa/gh-action-pypi-publish action performs the token exchange for you. You do not pass a password input.
Common errors
- Workflow filename mismatch. Renaming
publish.ymlbreaks the trust relationship until you update PyPI. - Missing
id-token: write. The job cannot obtain an identity token. - Publishing from a fork. The repository claim names the fork, so PyPI rejects it.
- Environment name differs.
pypion GitHub andreleaseon PyPI will not match.
Exercise
A release job fails with "invalid-publisher: valid token, but no corresponding publisher". The token is valid. Name two settings you would compare between PyPI and GitHub.
Show answer
Compare the workflow filename and the environment name. The owner and repository name are also valid answers. PyPI matches each of these against the claims in the OIDC token, and any difference causes this error.