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

  1. 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.
  2. When that workflow runs, it requests an OIDC token from the CI provider describing the run.
  3. 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.
  4. 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

The setup steps are in our PyPI trusted publishing how-to, and the broader shift across registries is discussed in our trends note.

Related pages