Skip to content

Commit 08efb23

Browse files
geroplona-agent
andcommitted
[npm] Add OIDC support for npm publishing
Use npm publish with --provenance flag when OIDC is available (GitHub Actions with id-token permission). This allows publishing without NPM_AUTH_TOKEN by using GitHub's OIDC token. Falls back to yarn publish with token-based auth when OIDC is not available for backward compatibility. Co-authored-by: Ona <no-reply@ona.com>
1 parent ddc1f04 commit 08efb23

1 file changed

Lines changed: 41 additions & 18 deletions

File tree

components/gitpod-protocol/scripts/publish.js

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@ if (process.env.DO_PUBLISH === "false") {
1919
process.exit(0);
2020
}
2121

22-
if (process.env.NPM_AUTH_TOKEN) {
22+
// Check if we should use OIDC (GitHub Actions with id-token permission)
23+
const useOIDC = process.env.ACTIONS_ID_TOKEN_REQUEST_URL && process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN;
24+
25+
if (useOIDC) {
26+
console.log("Using npm OIDC authentication (provenance)");
27+
} else if (process.env.NPM_AUTH_TOKEN) {
2328
fs.writeFileSync(
2429
path.join(pckDir, ".npmrc"),
2530
`//registry.npmjs.org/:_authToken=${process.env.NPM_AUTH_TOKEN}\n`,
2631
"utf-8",
2732
);
2833
} else {
29-
console.warn("NPM_AUTH_TOKEN env variable is not set");
34+
console.warn("NPM_AUTH_TOKEN env variable is not set and OIDC is not available");
3035
}
3136

3237
const pck = JSON.parse(fs.readFileSync(path.join(pckDir, "package.json"), "utf-8"));
@@ -35,19 +40,37 @@ fs.writeFileSync(path.join(pckDir, "package.json"), JSON.stringify(pck, undefine
3540

3641
const tag = qualifier.substr(0, qualifier.lastIndexOf("."));
3742

38-
child_process.execSync(
39-
[
40-
"yarn",
41-
"--cwd",
42-
pckDir,
43-
"publish",
44-
"--tag",
45-
tag,
46-
"--access",
47-
"public",
48-
"--ignore-scripts",
49-
"--network-timeout",
50-
"300000",
51-
].join(" "),
52-
{ stdio: "inherit" },
53-
);
43+
if (useOIDC) {
44+
// Use npm with provenance for OIDC
45+
child_process.execSync(
46+
[
47+
"npm",
48+
"publish",
49+
"--tag",
50+
tag,
51+
"--access",
52+
"public",
53+
"--provenance",
54+
"--ignore-scripts",
55+
].join(" "),
56+
{ stdio: "inherit", cwd: pckDir },
57+
);
58+
} else {
59+
// Fall back to yarn for token-based auth
60+
child_process.execSync(
61+
[
62+
"yarn",
63+
"--cwd",
64+
pckDir,
65+
"publish",
66+
"--tag",
67+
tag,
68+
"--access",
69+
"public",
70+
"--ignore-scripts",
71+
"--network-timeout",
72+
"300000",
73+
].join(" "),
74+
{ stdio: "inherit" },
75+
);
76+
}

0 commit comments

Comments
 (0)