Skip to content

Commit f989a1b

Browse files
chore(monorepo): Add Github action for set npm token (#2)
This pull request introduces a new custom GitHub Action for securely setting an npm publish token in the `.npmrc` configuration file, enabling automated publishing workflows. The action is defined with a YAML metadata file and implemented in JavaScript. **New GitHub Action for npm Token Management:** * Added `.github/actions/set-npm-token/action.yml` to define the action's metadata, including its name, description, required input (`npm-token`), and execution environment. * Implemented `.github/actions/set-npm-token/main.js` to read the npm token input, write it securely to the project's `.npmrc` file, and ensure npm lifecycle scripts can run as root. The script handles missing tokens and reports errors using GitHub Actions tooling.
1 parent f4802c5 commit f989a1b

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions
2+
name: 'Set npm token'
3+
description: 'This action sets a publish npm token (from an env var) in the .npmrc config file to be able to publish.'
4+
inputs:
5+
npm-token:
6+
description: 'npm publish token'
7+
required: true
8+
runs:
9+
using: 'node16'
10+
main: 'main.js'
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const core = require('@actions/core');
2+
const fs = require('fs');
3+
const path = require('path');
4+
5+
const main = async () => {
6+
const token = core.getInput('npm-token') || process.env.NPM_TOKEN;
7+
if (!token) {
8+
throw new Error('NPM_TOKEN env var not set');
9+
}
10+
11+
const lines = [
12+
// set npm auth token with publish permission from environment
13+
`//registry.npmjs.org/:_authToken=${token}`,
14+
15+
// this allows to execute npm lifecycle scripts by root
16+
'unsafe-perm = true',
17+
];
18+
19+
fs.writeFileSync(path.join(__dirname, '..', '..', '..', '.npmrc'), lines.join('\n'));
20+
};
21+
22+
main().catch((error) => {
23+
core.setFailed(error.message);
24+
});

0 commit comments

Comments
 (0)