Skip to content

Commit

Permalink
Add auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Danny McCormick committed Aug 6, 2019
1 parent fc9ff49 commit 5273d0d
Show file tree
Hide file tree
Showing 391 changed files with 79,850 additions and 45 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,47 @@ jobs:
- run: npm test
```
Publish to npmjs and GPR with npm:
```yaml
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@v1
with:
version: '10.x'
registry-url: 'https://registry.npmjs.org'
- run: npm install
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- uses: actions/setup-node@v1
with:
registry-url: 'https://npm.pkg.github.com'
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
Publish to npmjs and GPR with yarn:
```yaml
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@v1
with:
version: '10.x'
registry-url: <registry url>
- run: npm install -g yarn
- run: yarn install
- run: yarn publish
env:
NODE_AUTH_TOKEN: ${{ secrets.YARN_TOKEN }}
- uses: actions/setup-node@v1
with:
registry-url: 'https://npm.pkg.github.com'
- run: yarn publish
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
# License
The scripts and documentation in this project are released under the [MIT License](LICENSE)
Expand Down
21 changes: 21 additions & 0 deletions __tests__/__snapshots__/authutil.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`installer tests Appends trailing slash to registry 1`] = `
"//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}
registry=https://registry.npmjs.org/"
`;

exports[`installer tests Automatically configures GPR scope 1`] = `
"npm.pkg.github.com/:_authToken=\${NODE_AUTH_TOKEN}
@owner:registry=npm.pkg.github.com/"
`;

exports[`installer tests Configures scoped npm registries 1`] = `
"//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}
@myScope:registry=https://registry.npmjs.org/"
`;

exports[`installer tests Sets up npmrc for npmjs 1`] = `
"//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}
registry=https://registry.npmjs.org/"
`;
62 changes: 62 additions & 0 deletions __tests__/authutil.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import io = require('@actions/io');
import fs = require('fs');
import path = require('path');

const tempDir = path.join(
__dirname,
'runner',
path.join(
Math.random()
.toString(36)
.substring(7)
),
'temp'
);

const rcFile = path.join(tempDir, '.npmrc');

process.env['GITHUB_REPOSITORY'] = 'owner/repo';
process.env['RUNNER_TEMP'] = tempDir;
import * as auth from '../src/authutil';

describe('installer tests', () => {
beforeAll(async () => {
await io.rmRF(tempDir);
await io.mkdirP(tempDir);
}, 100000);

beforeEach(() => {
if (fs.existsSync(rcFile)) {
fs.unlinkSync(rcFile);
}
process.env['INPUT_SCOPE'] = '';
});

it('Sets up npmrc for npmjs', async () => {
await auth.configAuthentication('https://registry.npmjs.org/');
expect(fs.existsSync(rcFile)).toBe(true);
expect(fs.readFileSync(rcFile, {encoding: 'utf8'})).toMatchSnapshot();
});

it('Appends trailing slash to registry', async () => {
await auth.configAuthentication('https://registry.npmjs.org');

expect(fs.existsSync(rcFile)).toBe(true);
expect(fs.readFileSync(rcFile, {encoding: 'utf8'})).toMatchSnapshot();
});

it('Configures scoped npm registries', async () => {
process.env['INPUT_SCOPE'] = 'myScope';
await auth.configAuthentication('https://registry.npmjs.org');

expect(fs.existsSync(rcFile)).toBe(true);
expect(fs.readFileSync(rcFile, {encoding: 'utf8'})).toMatchSnapshot();
});

it('Automatically configures GPR scope', async () => {
await auth.configAuthentication('npm.pkg.github.com');

expect(fs.existsSync(rcFile)).toBe(true);
expect(fs.readFileSync(rcFile, {encoding: 'utf8'})).toMatchSnapshot();
});
});
13 changes: 2 additions & 11 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import os = require('os');
import path = require('path');

const toolDir = path.join(
process.cwd(),
__dirname,
'runner',
path.join(
Math.random()
Expand All @@ -14,7 +14,7 @@ const toolDir = path.join(
'tools'
);
const tempDir = path.join(
process.cwd(),
__dirname,
'runner',
path.join(
Math.random()
Expand All @@ -36,15 +36,6 @@ describe('installer tests', () => {
await io.rmRF(tempDir);
}, 100000);

afterAll(async () => {
try {
await io.rmRF(toolDir);
await io.rmRF(tempDir);
} catch {
console.log('Failed to remove test directories');
}
}, 100000);

it('Acquires version of node if no matching version is installed', async () => {
await installer.getNode('10.16.0');
const nodeDir = path.join(toolDir, 'node', '10.16.0', os.arch());
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ inputs:
version:
description: 'Version Spec of the version to use. Examples: 10.x, 10.15.1, >=10.15.0, lts'
default: '10.x'
registry-url:
description: 'Optional registry to set up for auth. Will set the registry in a project level .npmrc and .yarnrc file, and set up auth to read in from env.NODE_AUTH_TOKEN'
scope:
description: 'Optional scope for authenticating against scoped registries'
runs:
using: 'node12'
main: 'lib/setup-node.js'
52 changes: 52 additions & 0 deletions lib/authutil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
const core = __importStar(require("@actions/core"));
const github = __importStar(require("@actions/github"));
function configAuthentication(registryUrl) {
const npmrc = path.resolve(process.env['RUNNER_TEMP'] || process.cwd(), '.npmrc');
if (!registryUrl.endsWith('/')) {
registryUrl += '/';
}
writeRegistryToFile(registryUrl, npmrc);
}
exports.configAuthentication = configAuthentication;
function writeRegistryToFile(registryUrl, fileLocation) {
let scope = core.getInput('scope');
if (!scope && registryUrl.indexOf('npm.pkg.github.com') > -1) {
scope = github.context.repo.owner;
}
if (scope && scope[0] != '@') {
scope = '@' + scope;
}
core.debug(`Setting auth in ${fileLocation}`);
let newContents = '';
if (fs.existsSync(fileLocation)) {
const curContents = fs.readFileSync(fileLocation, 'utf8');
curContents.split(os.EOL).forEach((line) => {
// Add current contents unless they are setting the registry
if (!line.toLowerCase().startsWith('registry')) {
newContents += line + os.EOL;
}
});
}
// Remove http: or https: from front of registry.
const authString = registryUrl.replace(/(^\w+:|^)/, '') + ':_authToken=${NODE_AUTH_TOKEN}';
const registryString = scope
? `${scope}:registry=${registryUrl}`
: `registry=${registryUrl}`;
newContents += `${authString}${os.EOL}${registryString}`;
fs.writeFileSync(fileLocation, newContents);
core.exportVariable('NPM_CONFIG_USERCONFIG', fileLocation);
// Export empty node_auth_token so npm doesn't complain about not being able to find it
core.exportVariable('NODE_AUTH_TOKEN', 'XXXXX-XXXXX-XXXXX-XXXXX');
}
5 changes: 5 additions & 0 deletions lib/setup-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core"));
const installer = __importStar(require("./installer"));
const auth = __importStar(require("./authutil"));
const path = __importStar(require("path"));
function run() {
return __awaiter(this, void 0, void 0, function* () {
Expand All @@ -30,6 +31,10 @@ function run() {
// TODO: installer doesn't support proxy
yield installer.getNode(version);
}
const registryUrl = core.getInput('registry-url');
if (registryUrl) {
auth.configAuthentication(registryUrl);
}
// TODO: setup proxy from runner proxy config
const matchersPath = path.join(__dirname, '..', '.github');
console.log(`##[add-matcher]${path.join(matchersPath, 'tsc.json')}`);
Expand Down
15 changes: 15 additions & 0 deletions node_modules/.bin/which

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions node_modules/.bin/which.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions node_modules/@actions/github/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions node_modules/@actions/github/lib/context.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions node_modules/@actions/github/lib/context.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/@actions/github/lib/context.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5273d0d

Please sign in to comment.