Skip to content

Commit fe0d2c4

Browse files
authored
Make NPM_TOKEN optional for package release (#1744)
Classic tokens are no longer supported by npm. We may still want to run the release script from a local machine using a fine-grained access token, but these tokens expire after 90 days at most and are thus not suitable for our release process. I set up OpenID Connect between the `@webref/*` packages in npm and GitHub Actions and dropped the former `NPM_TOKEN` secret. This update adjusts the release script not to fail if such a token cannot be found. The call to `npmPublish` gets adjusted accordingly only to pass the token if it exists. That should close #1739.
1 parent 1d784db commit fe0d2c4

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

.github/workflows/release-package.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,3 @@ jobs:
3434
run: node tools/release-package.js ${{ github.event.pull_request.number }}
3535
env:
3636
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
37-
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

tools/release-package.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,14 @@ async function releasePackage(prNumber) {
8888

8989
console.log(`- Publish packages/${type} folder to npm`);
9090
const packageFolder = path.join(installFolder, "packages", type, "package.json");
91-
const pubResult = await npmPublish({
92-
package: packageFolder,
93-
token: NPM_TOKEN
91+
const pubOptions = {
92+
package: packageFolder
9493
//, debug: console.debug
95-
});
94+
};
95+
if (NPM_TOKEN) {
96+
pubOptions.token = NPM_TOKEN;
97+
}
98+
const pubResult = await npmPublish(pubOptions);
9699
console.log(`- Published version was ${pubResult.oldVersion}`);
97100
console.log(`- Version bump: ${pubResult.type}`);
98101
console.log(`- Published version is ${pubResult.version}`);
@@ -154,11 +157,12 @@ if (!GH_TOKEN) {
154157
process.exit(1);
155158
}
156159

160+
// An NPM token is needed to run the script from a local machine.
161+
// Authentication from a GitHub workflow rather relies on OpenID Connect
162+
// and the release workflow must be added as a trusted publisher for each
163+
// npm package that can be released, see:
164+
// https://docs.npmjs.com/trusted-publishers
157165
const NPM_TOKEN = config?.NPM_TOKEN ?? process.env.NPM_TOKEN;
158-
if (!NPM_TOKEN) {
159-
console.error("NPM_TOKEN must be set to an npm token as an env variable or in a config.json file");
160-
process.exit(1);
161-
}
162166

163167
// Note: npm-publish has a bug and needs an "INPUT_TOKEN" env variable:
164168
// https://github.com/JS-DevTools/npm-publish/issues/15

0 commit comments

Comments
 (0)