Setting GitHub as a trusted publisher for npm
So, stuff happened and npm has been updated to reduce the volume of stuff happening. In a world of SBOMs, SLSA, and supply chain attacks…
Setting GitHub as a trusted publisher for npm

So, stuff happened and npm has been updated to reduce the volume of stuff happening. In a world of SBOMs, SLSA, and supply chain attacks, it’s time to get serious about publishing packages. In this case, that means using the new Trusted Publisher feature to connect GitHub (or GitLab) to npm.
Set the trusted publisher on npm
- Sign into npm
- Select the package you want to set up, for example
astro-accelerator-utils - Click Settings
- In the Trusted Publishers section, select your provider, in my case it’s GitHub
- Enter you repository information:
Organization or user name, for example
Steve-FentonRepository name, for exampleastro-accelerator-utilsThe result should be thatSteve-Fenton/astro-accelerator-utilsmatches your repo in GitHub - Provide the workflow file name
This should match the workflow that will publish the package, in my case
build-astro.ymlThe file must be in.github/workflows/ - Click Set up connection
If you use environments, you can optionally limit publishing by environment.
Check you GitHub Action
In your permissions section, you need to allow id-token to be written.
permissions:
id-token: write
You can then use the npm publish step in your workflow.
I conditionally publish based on the version number, so I only publish when the version number changes.
- name: Publish if version has been updated
env:
NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
run: |
PACKAGE_NAME=$(node -p "require('./package.json').name")
LOCAL_VERSION=$(node -p "require('./package.json').version")
REMOTE_VERSION=$(npm view $PACKAGE_NAME version || echo "0.0.0")
if [ "$LOCAL_VERSION" != "$REMOTE_VERSION" ] && [ "$(printf '%s\n%s' "$REMOTE_VERSION" "$LOCAL_VERSION" | sort -V | tail -n1)" = "$LOCAL_VERSION" ]; then
echo "Local version $LOCAL_VERSION is higher than remote version $REMOTE_VERSION. Publishing..."
echo "//registry.npmjs.org/:_authToken=$NPM_AUTH_TOKEN" > ~/.npmrc
npm publish --access public
else
echo "Version $LOCAL_VERSION is not newer than $REMOTE_VERSION. Skipping publish."
fi
This is a more secure way to publish npm packages, but it’s also easier because you don’t need to keep updating tokens and secrets.
Originally published at https://stevefenton.co.uk on February 14, 2026.
메타데이터
- post_id
- 425e9d65e44d
- slug
- an-setting-github-as-a-trusted-publisher-for-npm-425e9d65e44d
- url
- https://medium.com/@steve.fenton/an-setting-github-as-a-trusted-publisher-for-npm-425e9d65e44d
- canonical_url
- https://medium.com/@steve.fenton/an-setting-github-as-a-trusted-publisher-for-npm-425e9d65e44d
- author_url
- https://medium.com/@steve.fenton
- status
- ok
- fetched_at
- 2026-07-24 06:21:58