Skip to content

Commit d8321bc

Browse files
committed
feat: add support for npm Trusted Publishers with NPM_ID_TOKEN
1 parent eccf474 commit d8321bc

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ release:
8989
9090
#### With Publishing
9191
92-
Before you can setup this action with publishing, you'll need to have an [npm token](https://docs.npmjs.com/creating-and-viewing-authentication-tokens) that can publish the packages in the repo you're setting up the action for and doesn't have 2FA on publish enabled ([2FA on auth can be enabled](https://docs.npmjs.com/about-two-factor-authentication)). You'll also need to [add it as a custom environment variable on your GitLab repo](https://docs.gitlab.com/ee/ci/variables/#custom-cicd-variables) with the name `NPM_TOKEN`. Once you've done that, you can create a file at `.gitlab-ci.yml` with the following content.
92+
There are two ways to authenticate with npm when publishing:
93+
94+
1. Trusted Publishers (recommended): Configure npm Trusted Publishers for your GitLab pipeline (see the npm docs: https://docs.npmjs.com/trusted-publishers#supported-cicd-providers). When the pipeline runs, npm will inject an `NPM_ID_TOKEN`, which this tool detects. In this mode you do NOT need to set `NPM_TOKEN`, and no `.npmrc` file is required—the npm CLI exchanges the identity token automatically.
95+
2. Classic Automation Token: Create an [npm automation token](https://docs.npmjs.com/creating-and-viewing-authentication-tokens) (without 2FA on publish) and add it as a [custom environment variable in GitLab](https://docs.gitlab.com/ee/ci/variables/#custom-cicd-variables) named `NPM_TOKEN`.
96+
97+
For any of the methods, create a file at `.gitlab-ci.yml` with the following content:
9398

9499
```yml
95100
stages:
@@ -114,13 +119,14 @@ release:
114119
INPUT_PUBLISH: yarn release
115120
```
116121

117-
By default the GitLab CI cli creates a `.npmrc` file with the following content:
122+
By default the GitLab CI cli creates a `.npmrc` file with the following content when `NPM_TOKEN` is present and no `.npmrc` exists (classic token mode):
118123

119124
```sh
120125
//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}
121126
```
122127

123128
However, if a `.npmrc` file is found, the GitLab CI cli does not recreate the file. This is useful if you need to configure the `.npmrc` file on your own.
129+
If `NPM_ID_TOKEN` is detected (Trusted Publishers), no `.npmrc` file is created or required.
124130
For example, you can add a step before running the Changesets GitLab CI cli:
125131

126132
```yml

src/main.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const main = async ({
2222
published,
2323
onlyChangesets,
2424
}: MainCommandOptions = {}) => {
25-
const { GITLAB_TOKEN, NPM_TOKEN } = env
25+
const { GITLAB_TOKEN, NPM_TOKEN, NPM_ID_TOKEN } = env
2626

2727
setOutput('published', false)
2828
setOutput('publishedPackages', [])
@@ -69,15 +69,20 @@ export const main = async ({
6969
const npmrcPath = `${env.HOME}/.npmrc`
7070
if (fs.existsSync(npmrcPath)) {
7171
console.log('Found existing .npmrc file')
72+
} else if (NPM_ID_TOKEN) {
73+
// Using npm Trusted Publishers: npm will exchange the OIDC token for a publish token internally.
74+
console.log(
75+
'Detected `NPM_ID_TOKEN`; skipping `.npmrc` creation (Trusted Publishers mode).',
76+
)
7277
} else if (NPM_TOKEN) {
73-
console.log('No .npmrc file found, creating one')
78+
console.log('No .npmrc file found, creating one with `NPM_TOKEN`')
7479
await fs.promises.writeFile(
7580
npmrcPath,
7681
`//registry.npmjs.org/:_authToken=${NPM_TOKEN}`,
7782
)
7883
} else {
7984
setFailed(
80-
'No `.npmrc` found nor `NPM_TOKEN` provided, unable to publish packages',
85+
'No `.npmrc` found and neither `NPM_TOKEN` nor `NPM_ID_TOKEN` provided, unable to publish packages',
8186
)
8287
return
8388
}

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ export type Env = GitLabCIPredefinedVariables &
2727

2828
HOME: string
2929
NPM_TOKEN?: string
30+
// Presence of NPM_ID_TOKEN indicates usage of npm Trusted Publishers;
31+
// when set we don't require nor create an .npmrc with NPM_TOKEN.
32+
NPM_ID_TOKEN?: string
3033
}
3134

3235
type MergeRequestVariables =

0 commit comments

Comments
 (0)