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

  1. Create a protected environment. In your repository settings, add an environment named release. Require a reviewer, or at least restrict it to tags matching v*. This is your human checkpoint.
  2. 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.
  3. 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.
  4. Grant only id-token: write to the publish job. Leave the workflow's default permissions read-only.
  5. Pin actions. Reference third-party actions by full commit SHA rather than a moving tag, and let a dependency bot propose updates.
  6. 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

Common mistakes

Related reading: the glossary defines provenance and attestation, and the resources page points to the npm Docs and the Python Packaging User Guide.

Related pages