mirror of
https://gitea.com/actions/setup-node.git
synced 2025-04-07 15:59:44 +00:00
Add auth support (#21)
* Updates * Update * Update * Update * Update * Yarn sometimes prefers npmrc, so use same token * Description * Update readme * Feedback * Add type * new toolkit and scoped registries * npmrc in RUNNER_TEMP * Dont always auth * Try exporting blank token * Get auth working for now pending runner changes * Fix string interpolation for auth token. * Don't export both userconfigs * Update authutil.js * Add single quotes for authString * Fix the registry string. * Use userconfig and append trailing slash * Keep in root of repo * Try just adding auth token * Remove auth token * Try changes again * Add tests * Npm and GPR samples * Add types
This commit is contained in:
52
lib/authutil.js
Normal file
52
lib/authutil.js
Normal file
@ -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');
|
||||
}
|
@ -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* () {
|
||||
@ -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')}`);
|
||||
|
Reference in New Issue
Block a user