Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/main.workflow
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
workflow "CI" {
on = "push"
resolves = ["Format", "Build"]
}

action "Dependencies" {
uses = "actions/[email protected]"
args = "install"
}

action "Build" {
needs = "Dependencies"
uses = "actions/[email protected]"
args = "run build"
}

action "Format" {
needs = "Dependencies"
uses = "actions/[email protected]"
args = "run format-check"
}
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This action sets by node environment for use in actions by:

- optionally downloading and caching a version of node - npm by version spec and add to PATH
- TODO: registering problem matchers for error output
- TODO: configuring authentication for npm packages
- optionally configuring authentication for npm packages
- TODO: configuring proxy if the runner is configured to use a proxy (coming with private runners)

# Usage
Expand Down Expand Up @@ -38,6 +38,17 @@ workflow:
- run: npm test
```

Auth:
```yaml
actions:
- uses: actions/setup-node@latest
with:
registryUrl: 'https://mycustomregistry.example.org'
registryToken: $ {{ token }}
authFile: 'optional/path/to/.npmrc/file'
- run: npm publish
```

# License

The scripts and documentation in this project are released under the [MIT License](LICENSE)
Expand Down
40 changes: 40 additions & 0 deletions lib/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"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 core = __importStar(require("@actions/core"));
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const os = __importStar(require("os"));
function setNpmrc(registryUrl, registryToken, authFile) {
let projectNpmrc = path.resolve(process.cwd(), '.npmrc');
if (authFile) {
projectNpmrc = path.resolve(process.cwd(), authFile);
}
let newContents = '';
if (fs.existsSync(projectNpmrc)) {
const curContents = fs.readFileSync(projectNpmrc, 'utf8');
curContents.split(os.EOL).forEach(line => {
// Add current contents unless they are setting the registry
if (!line.startsWith('registry')) {
newContents += line + os.EOL;
}
});
}
newContents +=
'registry=' +
registryUrl +
os.EOL +
'always-auth=true' +
os.EOL +
registryUrl +
':_authToken=${NPM_TOKEN}';
fs.writeFileSync(projectNpmrc, newContents);
core.exportSecret('NPM_TOKEN', registryToken);
}
exports.setNpmrc = setNpmrc;
3 changes: 1 addition & 2 deletions lib/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ function getNode(versionSpec) {
//
// prepend the tools path. instructs the agent to prepend for future tasks
//
// TODO - addPath not implemented yet (this should probably actually be in core)
// tc.addPath(toolPath);
core.addPath(toolPath);
});
}
exports.getNode = getNode;
Expand Down
7 changes: 7 additions & 0 deletions lib/setup-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core"));
const auth = __importStar(require("./auth"));
const installer = __importStar(require("./installer"));
function run() {
return __awaiter(this, void 0, void 0, function* () {
Expand All @@ -29,6 +30,12 @@ function run() {
// TODO: installer doesn't support proxy
yield installer.getNode(version);
}
const registryUrl = core.getInput('registryUrl');
if (registryUrl) {
const registryToken = core.getInput('registryToken', { required: true });
const authFile = core.getInput('authFile');
auth.setNpmrc(registryUrl, registryToken, authFile);
}
// TODO: setup proxy from runner proxy config
// TODO: problem matchers registered
}
Expand Down
24 changes: 12 additions & 12 deletions package-lock.json

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

37 changes: 37 additions & 0 deletions src/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as core from '@actions/core';
import * as path from 'path';
import * as fs from 'fs';
import * as os from 'os';

export function setNpmrc(
registryUrl: string,
registryToken: string,
authFile?: string
) {
let projectNpmrc: string = path.resolve(process.cwd(), '.npmrc');
if (authFile) {
projectNpmrc = path.resolve(process.cwd(), authFile);
}

let newContents = '';
if (fs.existsSync(projectNpmrc)) {
const curContents = fs.readFileSync(projectNpmrc, 'utf8');
curContents.split(os.EOL).forEach(line => {
// Add current contents unless they are setting the registry
if (!line.startsWith('registry')) {
newContents += line + os.EOL;
}
});
}
newContents +=
'registry=' +
registryUrl +
os.EOL +
'always-auth=true' +
os.EOL +
registryUrl +
':_authToken=${NPM_TOKEN}';
fs.writeFileSync(projectNpmrc, newContents);

core.exportSecret('NPM_TOKEN', registryToken);
}
3 changes: 1 addition & 2 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ export async function getNode(versionSpec: string) {
//
// prepend the tools path. instructs the agent to prepend for future tasks
//
// TODO - addPath not implemented yet (this should probably actually be in core)
// tc.addPath(toolPath);
core.addPath(toolPath);
}

async function queryLatestMatch(versionSpec: string): Promise<string> {
Expand Down
8 changes: 8 additions & 0 deletions src/setup-node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as core from '@actions/core';
import * as auth from './auth';
import * as installer from './installer';

async function run() {
Expand All @@ -13,6 +14,13 @@ async function run() {
await installer.getNode(version);
}

const registryUrl = core.getInput('registryUrl');
if (registryUrl) {
const registryToken = core.getInput('registryToken', {required: true});
const authFile = core.getInput('authFile');
auth.setNpmrc(registryUrl, registryToken, authFile);
}

// TODO: setup proxy from runner proxy config
// TODO: problem matchers registered
} catch (error) {
Expand Down