Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readDev Tool

Publish to npm from CI without tokens

Set up npm trusted publishing for a GitHub Actions release job and replace its long-lived publish token.

Your release job publishes an existing npm package from GitHub Actions. The job works as long as its npm token stays valid. Give npm the workflow's identity and the job can get a short-lived publishing credential during the run.

Register the workflow for one package

Open the package settings on npm and add a GitHub Actions trusted publisher. Use the owner and repository where the release runs. For .github/workflows/release.yml, enter release.yml as the workflow filename, including the extension.

If the job uses a GitHub environment, enter its name in the publisher settings to bind the trust to that environment. Give the connection permission for direct publishing if the settings ask which action to allow. See npm's trusted publishing guide for the package settings and runner requirements.

Open package.json before leaving the repo. Its repository.url needs to match the GitHub repository you're registering. This is worth checking when a package moved to a new organization and the release file moved with it.

Let the release job request an identity

Use a GitHub-hosted runner with Node 22.14 or later and npm CLI 11.5.1 or later. Check npm --version in the job because choosing a Node version alone may leave you with an older CLI. In the job that publishes, add id-token: write to the permissions. Keep contents: read if the job checks out the repository. If other jobs need tokens for different reasons, scope the new permission to the publishing job.

diagram

From workflow identity to publish

npm checks the GitHub identity against the workflow registered for this package before accepting the publish.

Keep your usual checkout, install, build and test steps. At the publish step, run npm publish against the npm registry with the old publish secret removed from the job or publish step. The npm CLI detects the OIDC environment and handles the exchange. If the install needs a private package, scope its read token to the install step.

Check the tarball before your next release

Run this from the directory containing the package you plan to ship, after building it.

npm pack --dry-run

The output lists the files your package would contain. Use it to catch a missing build directory or extra files before you cut a fresh version.

The publish itself checks the trust configuration. For an authentication error, compare the workflow filename and environment in npm with the job that ran, then check the job's token permission. Have the registry page open for the next release and confirm the new version appears before revoking the old publish token.