PyPI Trusted Publishing: What Changed for Python Maintainers
Python packages used to reach PyPI in one of two ways: a maintainer ran an upload tool such as twine with a password or API token, or CI did the same with a token stored as a secret. In 2023 PyPI introduced Trusted Publishing, which lets CI upload without any stored secret. Here is the background, the mechanics and what it changes in day-to-day maintenance.
Why PyPI made the change
API tokens improved on passwords, and PyPI's move to require two-factor authentication for all accounts, which took effect from the start of 2024, protected logins further. But a project-scoped token stored in CI is still a long-lived secret. It can leak through logs, misconfigured forks or a compromised third-party action, and whoever holds it can upload a new release. Trusted Publishing removes that secret by relying on short-lived identity tokens issued by the CI provider itself.
How it works, in four steps
- On PyPI, in the project's publishing settings, you add a trusted publisher: the CI provider, the repository owner and name, the workflow filename and, optionally, a deployment environment name.
- When that workflow runs, it requests an OIDC token from the CI provider describing the run.
- The upload step exchanges that token with PyPI for a temporary API token that is valid only for a short time and only for the configured project.
- The upload happens with the temporary token, which then expires.
PyPI also supports pending publishers: you can configure a trusted publisher for a project name that does not exist yet, so the very first release can be made from CI without ever creating a token. The pending publisher does not reserve the name; if someone else registers it first, the configuration is dropped.
Which CI systems
GitHub Actions was supported first. PyPI later added further providers, including GitLab CI/CD, Google Cloud and ActiveState. Check PyPI's documentation for the current list and for each provider's required settings, because the fields differ.
What to check before switching
- Workflow filename stability. The configuration names a specific workflow file. Renaming the file silently breaks publishing until you update PyPI.
- Use an environment. Naming a GitHub deployment environment such as
pypilets you add required reviewers, so a release needs a human approval. - Minimal permissions. Give
id-token: writeonly to the job that uploads, not to the job that runs tests or builds. - Remove old tokens. After a successful trusted release, delete the project-scoped API token from both PyPI and your CI secrets.
- Attestations. PyPI has since added support for digital attestations of published files (PEP 740); the official PyPA publishing action can generate them when used with Trusted Publishing.
The setup steps are in our PyPI trusted publishing how-to, and the broader shift across registries is discussed in our trends note.