-
Notifications
You must be signed in to change notification settings - Fork 0
Publish to npm via trusted publishing instead of NPM_TOKEN #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,7 +1,8 @@ | ||||||||||||||||||
| name: Release | ||||||||||||||||||
|
|
||||||||||||||||||
| # Publishes to npm when a GitHub Release is published. | ||||||||||||||||||
| # See RELEASING.md for the full process and required NPM_TOKEN secret. | ||||||||||||||||||
| # Authenticates to npm as a trusted publisher via OIDC (no token needed). | ||||||||||||||||||
| # See RELEASING.md for the full process. | ||||||||||||||||||
| on: | ||||||||||||||||||
| release: | ||||||||||||||||||
| types: [published] | ||||||||||||||||||
|
|
@@ -21,6 +22,10 @@ jobs: | |||||||||||||||||
| node-version: 20 | ||||||||||||||||||
| registry-url: 'https://registry.npmjs.org' | ||||||||||||||||||
|
|
||||||||||||||||||
| # Trusted publishing via OIDC requires npm >= 11.5.1; Node 20 ships npm 10. | ||||||||||||||||||
|
Comment on lines
22
to
+25
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Latest NPM version is 12.0.2 and it requires atleast Node.js 22.22.2 https://github.com/npm/cli/blob/51c2bf81fa2c31547d0fec44fff2aaac3d9a9862/package.json#L265, so this will error out. We should use
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. altis-cli/.github/workflows/ci.yml Line 19 in efd7995
|
||||||||||||||||||
| - name: Upgrade npm | ||||||||||||||||||
| run: npm install -g npm@latest | ||||||||||||||||||
|
|
||||||||||||||||||
| - run: npm ci | ||||||||||||||||||
|
|
||||||||||||||||||
| - name: Verify tag matches package.json version | ||||||||||||||||||
|
|
@@ -42,5 +47,3 @@ jobs: | |||||||||||||||||
|
|
||||||||||||||||||
| - name: Publish to npm | ||||||||||||||||||
| run: npm publish --provenance --access public | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Under trusted publishing, npm generates provenance on its own. From [npm's trusted publishers docs](https://docs.npmjs.com/trusted-publishers):
The docs list three conditions for the automatic behaviour: publishing via trusted publishing (OIDC), from a public repository, and a public package. All three are true for - run: npm publish --access publicWe don't necessarilty need to change it thoug, keeping the flag is harmless and makes the intent obvious to anyone reading the workflow. I'm just flagging it only so we know it's no longer doing any work. |
||||||||||||||||||
| env: | ||||||||||||||||||
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | ||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,17 +47,14 @@ Publishing the release triggers `.github/workflows/release.yml`, which: | |
| - runs `npm audit` (advisory — does not block the release), | ||
| - publishes to npm with [provenance](https://docs.npmjs.com/generating-provenance-statements). | ||
|
|
||
| ## One-time setup: the `NPM_TOKEN` secret | ||
| ## One-time setup: trusted publishing | ||
|
|
||
| Publishing needs an npm access token stored as a repository secret named | ||
| `NPM_TOKEN`: | ||
| Publishing authenticates to npm as a | ||
| [trusted publisher](https://docs.npmjs.com/trusted-publishers) via OIDC — no | ||
| token or repository secret is required. | ||
|
|
||
| 1. On [npmjs.com](https://www.npmjs.com/) → **Access Tokens** → **Generate New | ||
| Token** → **Granular Access Token** (recommended) with **Read and write** | ||
| permission scoped to the `altis-cli` package. Use an **Automation** token so | ||
| it bypasses 2FA in CI. | ||
| 2. In GitHub: **Settings → Secrets and variables → Actions → New repository | ||
| secret**, name it `NPM_TOKEN`, and paste the token. | ||
| The workflow grants `id-token: write` (for the OIDC exchange) and upgrades npm | ||
| to a version new enough to support trusted publishing. | ||
|
Comment on lines
+50
to
+57
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new section says no token or secret is needed, which is true, but it doesn't say what someone does need to set up. If the trusted publisher config is ever lost, or someone sets this up for another package, there's nothing here to follow. Worth spelling out, because npm matches these fields exactly and they're case-sensitive:
One thing to watch on step 5: the repo has no GitHub environments configured, so if anything is entered in npm's Environment field the publish will fail. Also worth adding a line that the old npm automation token should be revoked on npmjs.com — this PR stops using it, but the token itself is still live until someone deletes it. |
||
|
|
||
| Provenance additionally requires the repository to be public and the | ||
| `repository` field in `package.json` to be set (both already true). | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems
registry-urlmay stop the OIDC login from happening. Whenactions/setup-nodeis givenregistry-url, it always writes this line into.npmrc:Now that we've removed the secret,
NODE_AUTH_TOKENis empty and older versions of the action even filled it with a dummy placeholder value. Either way npm sees an auth line, assumes we've already logged in with a token, and never does the OIDC exchange with npm. The publish then fails withENEEDAUTHor a 404. People hit this on npm 11.14.1:This has since been fixed, but only in setup-node v7 — actions/setup-node#1558 removed the dummy
NODE_AUTH_TOKENexport, and it shipped in https://github.com/actions/setup-node/releases/tag/v7.0.0 (which also added official Trusted Publisher docs). We're on@v4, so we don't have it.We can fix this in one of two ways:
registry-url:registry-url—registry.npmjs.orgis the default anyway, and--access publicstill works: