Skip to content

npm trusted publishing with OIDC

Publish private npm packages from GitHub Actions or GitLab CI with no stored publisher key. The CI job exchanges its OIDC token for a publisher key that expires after 15 minutes.

Overview

This guide tells you how to publish a private npm package from GitHub Actions or GitLab CI with trusted publishing. With trusted publishing, the CI job stores no publisher key. The CI provider gives the job a signed OIDC token. The npm CLI sends that token to the registry and receives a publisher key that expires after 15 minutes.

OIDC (OpenID Connect) is a standard for signed identity tokens. The token tells the registry which repository, workflow file and environment started the job. The registry compares these claims with the trusted publisher of the package. If the claims agree, the registry gives the job a key that can publish that one package only.

This guide is for maintainers who release packages from CI. The instructions apply to packages on PrivateNPM.com. For packages on registry.npmjs.org, use the npm documentation about trusted publishing.

Before you start

  • Package. The package must exist in the registry. Publish the first version with a publisher key. Refer to the guide about how to publish a private npm package. Trusted publishing can publish all later versions.
  • Role. You must have the Admin role. To configure a trusted publisher, you must have the registry:credential:manage and npm:package:publish permissions.
  • npm CLI. The CI job must use npm CLI 11.5.1 or newer. Older versions cannot do the OIDC exchange. Some runner images contain an older npm CLI.
  • CI values. Write down the repository owner, the repository name and the path of the workflow file that runs npm publish.
  • Plan. Trusted publishing has no plan gate. The publish checks of your plan and trial still apply.

How it works

A trusted publish has four steps:

  1. The CI provider issues an OIDC ID token to the job.
  2. The npm CLI sends the ID token to the exchange endpoint of the registry.
  3. The registry verifies the token and returns a publisher key.
  4. The npm CLI publishes with that key.

The ID token

The ID token is a JSON Web Token (JWT) that the CI provider signs. The registry gets the public keys of the provider from the OpenID configuration of the issuer. The registry accepts only RS256 signatures. The registry also examines the expiry time and the subject of the token.

ProviderDefault issuer
GitHub Actionshttps://token.actions.githubusercontent.com
GitLab CIhttps://gitlab.com

For GitHub Enterprise Server or a self-managed GitLab, type your own issuer in the OIDC issuer (optional) field. The issuer must be an HTTPS URL.

The audience

The aud claim names the registry that the token is for. The registry requires npm: followed by the registry hostname. On the shared registry, the audience is npm:registry.privatenpm.com. On the custom domain npm.acme.com, the audience is npm:npm.acme.com. A token for a different registry does not work on this registry.

On GitHub Actions, the npm CLI requests the token with the correct audience. The CLI uses the host of the registry URL in your configuration. On GitLab CI, you write the audience in the job.

The claims that the registry compares

FieldGitHub claimGitLab claim
Repository owner and Repositoryrepositoryproject_path
Workflow filejob_workflow_ref, else workflow_refci_config_ref_uri
Environment (optional)environmentenvironment
Repository IDrepository_idproject_id

The comparison ignores case. The registry removes the repository prefix and the Git ref from the workflow claim. Then the registry compares the path that is left. Thus the Workflow file field must contain the full path, for example .github/workflows/publish.yml. The file name alone does not match.

The registry records the repository ID from the first exchange that succeeds. After that, the ID must agree. Thus a new repository with the old name cannot publish. If you save the configuration again with the same owner and repository, the registry keeps the ID. If you change the owner or the repository, the registry records the new ID at the next exchange.

The registry does not compare the branch or the tag. To limit the branches, use an environment.

The exchange

The npm CLI sends this request to the registry host:

POST /-/npm/v1/oidc/token/exchange/package/@acme%2Fsdk
Authorization: Bearer <OIDC ID token>

If all checks pass, the registry returns {"token":"prt_…"}. For each failure, the registry returns 401 with the message oidc token exchange failed. The registry does not tell the client which check failed. Thus the response tells an attacker nothing about the configuration. Each client address can make 30 attempts in 15 minutes. After that, the registry returns 429.

The publisher key

The exchanged key is a usual publisher key with the prt_ prefix. The key has these limits:

  • The key expires after 15 minutes.
  • The key is valid for one registry and one package.
  • The key can publish versions. If the member can update the package, the key can also change dist-tags.
  • The key cannot read packages or unpublish versions.
  • The key acts as the member who configured the trusted publisher.

The registry checks the membership and the publish permission of that member before each exchange. The registry checks the membership again each time the CI job uses the key. If the member leaves the organization or loses the publish permission, publishes from CI stop.

NOTE: The member who saves the configuration is the member that the keys act as. When a different member saves the configuration, that member becomes the member that the keys act as.

Configure the trusted publisher

A package has one trusted publisher. Make sure that the package exists and that you have the Admin role.

  1. Open Registry → Packages.
  2. Open the package.
  3. In the Trusted publishing card, click Set up trusted publishing or Change trusted publisher. The Edit package page opens at the Trusted publishing section.
  4. Select GitHub Actions or GitLab CI in Provider.
  5. Type the GitHub owner or the GitLab namespace path in Repository owner, for example acme.
  6. Type the repository name in Repository, for example sdk.
  7. Type the path of the workflow file in Workflow file.
  8. If the job uses a CI environment, type its name in Environment (optional).
  9. For self-managed CI only, type the issuer URL in OIDC issuer (optional).
  10. Click Save trusted publisher. The message "Trusted publisher saved." appears.

For GitHub, the workflow file is the file below .github/workflows/. For GitLab, the workflow file is usually .gitlab-ci.yml. For a GitLab group with subgroups, type the full namespace path, for example acme/platform.

After you save, the package page shows a CI snippet with the correct audience. The management API offers the same configuration at PUT /api/v1/packages/{packageId}/trusted-publisher. The body contains provider, repositoryOwner, repositoryName and workflow, and optionally environment and issuer. Refer to the API reference.

Publish from GitHub Actions

The job must have the id-token: write permission. Without this permission, GitHub does not issue an ID token to the job. The project .npmrc file contains the scope route and no credential line:

@acme:registry=https://registry.privatenpm.com/

The workflow file .github/workflows/publish.yml:

name: Publish
on:
  release:
    types: [published]
jobs:
  publish:
    runs-on: ubuntu-latest
    environment: release
    permissions:
      id-token: write # mint the OIDC token npm exchanges
      contents: read
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 24
      - run: npm install -g npm@11
      - run: npm ci
      - run: npm publish

The step npm install -g npm@11 makes sure that the CLI is 11.5.1 or newer. If you remove the environment line, also leave the Environment (optional) field empty.

Reusable workflows

A reusable workflow is a workflow file that a different workflow calls with uses. In a called workflow, GitHub writes the called file in the job_workflow_ref claim. Thus you configure the called workflow, not the caller. The repository fields contain the repository that calls the workflow.

# .github/workflows/release.yml in acme/sdk
jobs:
  release:
    permissions:
      id-token: write
      contents: read
    uses: acme/ci-workflows/.github/workflows/npm-publish.yml@v1

For this example, the fields are Repository owner acme, Repository sdk and Workflow file .github/workflows/npm-publish.yml. The caller must give the id-token: write permission.

CAUTION: Call reusable workflows only from repositories that you control. The registry compares the path of the called file, not the repository that holds the file.

Publish from GitLab CI

GitLab issues the ID token as a CI/CD variable. The npm CLI reads the token from the variable NPM_ID_TOKEN. Write the audience in the aud key:

publish:
  image: node:24
  environment: release
  rules:
    - if: $CI_COMMIT_TAG
  id_tokens:
    NPM_ID_TOKEN:
      aud: npm:registry.privatenpm.com
  script:
    - npm install -g npm@11
    - npm ci
    - npm publish

The registry refuses a token when the pipeline configuration is not in the project. The ci_config_ref_uri claim of such a token is empty.

Custom domains

On a private registry, the audience contains the custom domain. Change the scope route to the custom domain:

@acme:registry=https://npm.acme.com/

On GitHub Actions, no other change is necessary. On GitLab CI, change the audience to npm:npm.acme.com. If the registry moves to a new hostname, update the .npmrc file and the GitLab audience at the same time. Refer to change the hostname of a registry.

Verify the result

  1. Increase the version in package.json to a version that does not exist.
  2. Start the workflow from the branch, tag or environment that you configured.
  3. Examine the log of the publish step. The last line contains + @acme/sdk@ and the new version.
  4. Run npm view @acme/sdk versions with a download key. The list contains the new version.
  5. Open the package page. The activity of the package shows the publish with the repository, the commit, the run number and the member.

Do the test before you remove the old publisher key from the CI secrets.

Troubleshoot

For an E401 during npm ci, start with the E401 checklist. Trusted publishing does not authenticate dependency installs.

SymptomCauseCorrection
npm publish shows ENEEDAUTH or E401The npm CLI is older than 11.5.1, or the job has no ID token.Install npm 11. Add id-token: write, or the NPM_ID_TOKEN entry in GitLab.
401 oidc token exchange failedA claim does not agree with the configuration.Compare the owner, the repository, the full workflow path and the environment.
401 on a custom domain onlyThe audience names a different host.Use the custom domain in the scope route and in the GitLab audience.
401 after a repository changeThe repository ID changed, or the repository has a new name.Save the trusted publisher again with the current values. If you deleted the repository and made a new one with the same name, remove the trusted publisher and save a new one.
401 for a new packageThe package does not exist.Publish the first version with a publisher key.
401 for all runs after a staff changeThe member who configured the trusted publisher left, or lost the publish permission.A current member with the publish permission saves the trusted publisher again.
429 from the exchangeMore than 30 attempts in 15 minutes from one address.Wait 15 minutes. Correct the configuration before more attempts.
400 on npm publishThe publish contains a provenance attestation.Remove --provenance and publishConfig.provenance.
403 after the exchangeThe version exists, or the plan does not permit the publish. The message tells you the cause.Use a new version, or examine your plan.

The registry accepts no provenance attestations. The npm CLI skips automatic provenance on this registry. Never paste ID tokens, keys or logs that contain them into a support request.

Security and maintenance

  • Environments. Protect the release environment with branch rules and required reviewers in GitHub or GitLab. Then type the environment name in the trusted publisher. The registry does not examine branches.
  • Remove old keys. After the first trusted publish succeeds, revoke the publisher key that CI used before.
  • Exchanged keys. Exchanged keys appear in Registry → Registry keys with names such as trusted publishing github acme/sdk. The keys expire after 15 minutes.
  • Removal. Click Remove trusted publisher on the edit page. The next exchange fails. Keys that the registry issued before stay valid until they expire.
  • Audit. The audit log records each change with npm_trusted_publisher.created, .updated or .deleted. The audit log records each exchange with npm_trusted_publisher.accessed.
  • Webhooks. The three change events are also webhook events on Growth and higher plans. Exchanges send no webhook. Refer to the webhooks guide.
  • Local releases. Keep one publisher key in a password manager for a release that CI cannot do.

WARNING: Do not write the ID token or the exchanged key to the job log. The registry does not block a second use of an ID token. A person with a copied token can get a publisher key for the package until the ID token expires.

WARNING: Do not give write access to the workflow file to people who cannot publish. A person who can change the workflow file can publish the package.

Keep the release working

Install the release from CI in a clean project. Then examine the download activity of the package.

You need an organization to do these steps. Start a 30-day trial. Before you add a card, you can publish 3 packages and store 10 GB of content.

READY WHEN YOU ARE

Put private packages to work.