Building a Tag-Triggered Trusted Publishing Workflow
The goal of this tutorial is a release pipeline where no human holds a registry token at all. You push a version tag, CI builds the artifact once, and the registry accepts it because it trusts the identity of that specific workflow. We use GitHub Actions in the examples because both npm and PyPI document it, but the shape carries over to other CI systems that issue OIDC tokens.
What "trusted" means here
Classic publishing uses a secret API token stored in CI. Anyone who can read that secret, or trick a workflow into printing it, can publish as you. With trusted publishing, the CI provider issues a short-lived OpenID Connect token describing the run: which repository, which workflow file, which environment. The registry exchanges that for a temporary upload credential. PyPI calls this Trusted Publishing; npm supports OIDC-based publishing and can attach provenance statements built on Sigstore. Check each registry's current documentation for the exact options available to your account.
Step-by-step
- Create a protected environment. In your repository settings, add an environment named
release. Require a reviewer, or at least restrict it to tags matchingv*. This is your human checkpoint. - Register the publisher on the registry. On PyPI, open the project's publishing settings and add a trusted publisher with the repository owner, repository name, workflow filename and environment name. On npm, configure the package's trusted publisher settings in the same spirit. The names must match exactly; a typo is the most common reason the first run fails.
- Split build from publish. One job builds and uploads the artifact; a second job, bound to the environment, downloads it and publishes. The publish job never runs your test or build scripts, which keeps third-party code away from the credential.
- Grant only
id-token: writeto the publish job. Leave the workflow's default permissions read-only. - Pin actions. Reference third-party actions by full commit SHA rather than a moving tag, and let a dependency bot propose updates.
- Delete the old tokens. Once a release succeeds, revoke the long-lived tokens you used before. Leaving them around undoes most of the benefit.
Worked example: a PyPI workflow
name: release
on:
push:
tags: ["v*"]
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
# fetch the tagged commit with the standard repository-clone action, pinned by SHA
- uses: actions/setup-python@<sha>
with: { python-version: "3.12" }
- run: python -m pip install build
- run: python -m build
- uses: actions/upload-artifact@<sha>
with: { name: dist, path: dist/ }
publish:
needs: build
runs-on: ubuntu-latest
environment: release
permissions:
id-token: write
steps:
- uses: actions/download-artifact@<sha>
with: { name: dist, path: dist/ }
- uses: pypa/gh-action-pypi-publish@<sha>
For an npm package the publish job looks similar: set up Node with the registry URL, download the packed tarball and run npm publish --provenance --access public against it. Publishing the tarball you already built, rather than running npm publish from a fresh clone, guarantees the bytes you tested are the bytes users install.
How to check it worked
- The package page on the registry shows the release linked to your repository and workflow.
- On npm,
npm audit signaturesrun in a project that installs your package reports verified provenance. - Your repository secrets list no longer contains a registry token.
Common mistakes
- Environment name mismatch. The registry config says
release, the workflow saysRelease. The exchange fails with a vague permission error. - Workflow renamed. Trusted publishers are tied to the workflow filename. Rename the file and you must update the registry settings.
- Tag pushed before the version bump. Add a step that fails the build if the tag does not equal the version in
package.jsonorpyproject.toml. - Running on pull requests. Never let a workflow triggered by forks reach the publish job.
Related reading: the glossary defines provenance and attestation, and the resources page points to the npm Docs and the Python Packaging User Guide.