Skip to content

Commit 6496446

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 6496446

1 file changed

Lines changed: 36 additions & 18 deletions

File tree

components/gitpod-protocol/scripts/publish.js

Lines changed: 36 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,32 @@ 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+
// Build publish command arguments
44+
const publishArgs = useOIDC
45+
? [
46+
"npm",
47+
"publish",
48+
"--tag",
49+
tag,
50+
"--access",
51+
"public",
52+
"--provenance",
53+
"--ignore-scripts",
54+
]
55+
: [
56+
"yarn",
57+
"--cwd",
58+
pckDir,
59+
"publish",
60+
"--tag",
61+
tag,
62+
"--access",
63+
"public",
64+
"--ignore-scripts",
65+
"--network-timeout",
66+
"300000",
67+
];
68+
69+
const execOptions = useOIDC ? { stdio: "inherit", cwd: pckDir } : { stdio: "inherit" };
70+
71+
child_process.execSync(publishArgs.join(" "), execOptions);

0 commit comments

Comments
 (0)