mirror of
https://github.com/actions/setup-node.git
synced 2025-11-07 13:06:55 +00:00
Compare commits
5 commits
1da64d3170
...
d2d5129530
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d2d5129530 | ||
|
|
dda4788290 | ||
|
|
55b7d827be | ||
|
|
473cb1b506 | ||
|
|
574c6daa52 |
3 changed files with 58 additions and 0 deletions
|
|
@ -7,6 +7,7 @@ import * as auth from '../src/authutil';
|
||||||
import * as cacheUtils from '../src/cache-utils';
|
import * as cacheUtils from '../src/cache-utils';
|
||||||
|
|
||||||
let rcFile: string;
|
let rcFile: string;
|
||||||
|
let pkgJson: string;
|
||||||
|
|
||||||
describe('authutil tests', () => {
|
describe('authutil tests', () => {
|
||||||
const _runnerDir = path.join(__dirname, 'runner');
|
const _runnerDir = path.join(__dirname, 'runner');
|
||||||
|
|
@ -25,10 +26,12 @@ describe('authutil tests', () => {
|
||||||
process.env['GITHUB_REPOSITORY'] = 'OwnerName/repo';
|
process.env['GITHUB_REPOSITORY'] = 'OwnerName/repo';
|
||||||
process.env['RUNNER_TEMP'] = tempDir;
|
process.env['RUNNER_TEMP'] = tempDir;
|
||||||
rcFile = path.join(tempDir, '.npmrc');
|
rcFile = path.join(tempDir, '.npmrc');
|
||||||
|
pkgJson = path.join(tempDir, 'package.json');
|
||||||
}, 100000);
|
}, 100000);
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await io.rmRF(rcFile);
|
await io.rmRF(rcFile);
|
||||||
|
await io.rmRF(pkgJson);
|
||||||
// if (fs.existsSync(rcFile)) {
|
// if (fs.existsSync(rcFile)) {
|
||||||
// fs.unlinkSync(rcFile);
|
// fs.unlinkSync(rcFile);
|
||||||
// }
|
// }
|
||||||
|
|
@ -113,6 +116,15 @@ describe('authutil tests', () => {
|
||||||
expect(rc['always-auth']).toBe('false');
|
expect(rc['always-auth']).toBe('false');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Automatically configures npm scope from package.json', async () => {
|
||||||
|
process.env['INPUT_SCOPE'] = '';
|
||||||
|
fs.writeFileSync(pkgJson, '{"name":"@myscope/mypackage"}');
|
||||||
|
await auth.configAuthentication('https://registry.npmjs.org', '');
|
||||||
|
|
||||||
|
const rc = readRcFile(rcFile);
|
||||||
|
expect(rc['@myscope:registry']).toBe('https://registry.npmjs.org/');
|
||||||
|
});
|
||||||
|
|
||||||
it('Sets up npmrc for always-auth true', async () => {
|
it('Sets up npmrc for always-auth true', async () => {
|
||||||
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
|
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
|
||||||
expect(fs.statSync(rcFile)).toBeDefined();
|
expect(fs.statSync(rcFile)).toBeDefined();
|
||||||
|
|
|
||||||
|
|
@ -300,6 +300,35 @@ steps:
|
||||||
- run: npm test
|
- run: npm test
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**Restore-Only Cache**
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
## In some workflows, you may want to restore a cache without saving it. This can help reduce cache writes and storage usage in workflows that only need to read from cache
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v5
|
||||||
|
# Restore Node.js modules cache (restore-only)
|
||||||
|
- name: Restore Node modules cache
|
||||||
|
uses: actions/cache@v4
|
||||||
|
id: cache-node-modules
|
||||||
|
with:
|
||||||
|
path: ~/.npm
|
||||||
|
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-node-
|
||||||
|
# Setup Node.js
|
||||||
|
- name: Setup Node.js
|
||||||
|
uses: actions/setup-node@v6
|
||||||
|
with:
|
||||||
|
node-version: '24'
|
||||||
|
# Install dependencies
|
||||||
|
- run: npm install
|
||||||
|
```
|
||||||
|
|
||||||
|
> For more details related to cache scenarios, please refer [Node – npm](https://github.com/actions/cache/blob/main/examples.md#node---npm).
|
||||||
|
|
||||||
## Multiple Operating Systems and Architectures
|
## Multiple Operating Systems and Architectures
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,12 @@ function writeRegistryToFile(
|
||||||
if (!scope && registryUrl.indexOf('npm.pkg.github.com') > -1) {
|
if (!scope && registryUrl.indexOf('npm.pkg.github.com') > -1) {
|
||||||
scope = github.context.repo.owner;
|
scope = github.context.repo.owner;
|
||||||
}
|
}
|
||||||
|
if (!scope) {
|
||||||
|
const namePrefix = packageJson('name')?.match(/^(@[^/]+)\//);
|
||||||
|
if (namePrefix) {
|
||||||
|
scope = namePrefix[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
if (scope && scope[0] != '@') {
|
if (scope && scope[0] != '@') {
|
||||||
scope = '@' + scope;
|
scope = '@' + scope;
|
||||||
}
|
}
|
||||||
|
|
@ -57,3 +63,14 @@ function writeRegistryToFile(
|
||||||
process.env.NODE_AUTH_TOKEN || 'XXXXX-XXXXX-XXXXX-XXXXX'
|
process.env.NODE_AUTH_TOKEN || 'XXXXX-XXXXX-XXXXX-XXXXX'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function packageJson(prop: string){
|
||||||
|
const pkgPath: string = path.resolve(process.env['RUNNER_TEMP'] || process.cwd(), 'package.json');
|
||||||
|
try {
|
||||||
|
const json = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
||||||
|
|
||||||
|
return prop ? json[prop] : json;
|
||||||
|
} catch(e) {
|
||||||
|
core.debug(`Unable to read from package.json`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue