feat: add support for a 2nd package registry

This commit is contained in:
Keats Kirsch 2023-11-03 10:38:20 -04:00
parent 8f152de45c
commit 247e682820
4 changed files with 79 additions and 38 deletions

View file

@ -21,6 +21,13 @@ inputs:
token:
description: Used to pull node distributions from node-versions. Since there's a default, this is typically not supplied by the user. When running this action on github.com, the default value is sufficient. When running on GHES, you can pass a personal access token for github.com if you are experiencing rate limiting.
default: ${{ github.server_url == 'https://github.com' && github.token || '' }}
registry-url2:
description: 'Optional 2nd 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_TOKEN2.'
scope2:
description: 'Optional 2nd scope for authenticating against scoped registries. Will fall back to the repository owner when using the GitHub Packages registry (https://npm.pkg.github.com/).'
token2:
description: Optional 2nd token for authenticating against a 2nd registry. When running this action on github.com, the default value is sufficient. When running on GHES, you can pass a personal access token for github.com if you are experiencing rate limiting.
default: ${{ github.server_url == 'https://github.com' && github.token || '' }}
cache:
description: 'Used to specify a package manager for caching in the default directory. Supported values: npm, yarn, pnpm.'
cache-dependency-path:

52
dist/setup/index.js vendored
View file

@ -92462,31 +92462,25 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result;
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.configAuthentication = void 0;
exports.configAuthentication2 = exports.configAuthentication = void 0;
const fs = __importStar(__nccwpck_require__(7147));
const os = __importStar(__nccwpck_require__(2037));
const path = __importStar(__nccwpck_require__(1017));
const core = __importStar(__nccwpck_require__(2186));
const github = __importStar(__nccwpck_require__(5438));
function configAuthentication(registryUrl, alwaysAuth) {
const npmrc = path.resolve(process.env['RUNNER_TEMP'] || process.cwd(), '.npmrc');
writeRegistryToFile(registryUrl, alwaysAuth, 'NODE_AUTH_TOKEN');
}
exports.configAuthentication = configAuthentication;
function getFileLocation() {
return path.resolve(process.env['RUNNER_TEMP'] || process.cwd(), '.npmrc');
}
function writeRegistryToFile(registryUrl, alwaysAuth, tokenEnvVar) {
const fileLocation = getFileLocation();
if (!registryUrl.endsWith('/')) {
registryUrl += '/';
}
writeRegistryToFile(registryUrl, npmrc, alwaysAuth);
}
exports.configAuthentication = configAuthentication;
function writeRegistryToFile(registryUrl, fileLocation, alwaysAuth) {
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;
}
if (scope) {
scope = scope.toLowerCase() + ':';
}
const scope = getScope('scope', registryUrl);
core.debug(`Setting auth in ${fileLocation}`);
let newContents = '';
if (fs.existsSync(fileLocation)) {
@ -92499,15 +92493,32 @@ function writeRegistryToFile(registryUrl, fileLocation, alwaysAuth) {
});
}
// Remove http: or https: from front of registry.
const authString = registryUrl.replace(/(^\w+:|^)/, '') + ':_authToken=${NODE_AUTH_TOKEN}';
const authString = registryUrl.replace(/(^\w+:|^)/, '') + ':_authToken=${' + tokenEnvVar + '}';
const registryString = `${scope}registry=${registryUrl}`;
const alwaysAuthString = `always-auth=${alwaysAuth}`;
newContents += `${authString}${os.EOL}${registryString}${os.EOL}${alwaysAuthString}`;
fs.writeFileSync(fileLocation, newContents);
core.exportVariable('NPM_CONFIG_USERCONFIG', fileLocation);
// Export empty node_auth_token if didn't exist so npm doesn't complain about not being able to find it
core.exportVariable('NODE_AUTH_TOKEN', process.env.NODE_AUTH_TOKEN || 'XXXXX-XXXXX-XXXXX-XXXXX');
core.exportVariable(tokenEnvVar, process.env[tokenEnvVar] || 'XXXXX-XXXXX-XXXXX-XXXXX');
}
function getScope(scopeKey, registryUrl) {
let scope = core.getInput(scopeKey);
if (!scope && registryUrl.indexOf('npm.pkg.github.com') > -1) {
scope = github.context.repo.owner;
}
if (scope && scope[0] != '@') {
scope = '@' + scope;
}
if (scope) {
scope = scope.toLowerCase() + ':';
}
return scope;
}
function configAuthentication2(registryUrl2, alwaysAuth2) {
writeRegistryToFile(registryUrl2, alwaysAuth2, 'NODE_AUTH_TOKEN2');
}
exports.configAuthentication2 = configAuthentication2;
/***/ }),
@ -93697,6 +93708,11 @@ function run() {
const alwaysAuth = core.getInput('always-auth');
if (registryUrl) {
auth.configAuthentication(registryUrl, alwaysAuth);
const registryUrl2 = core.getInput('registry-url2');
if (registryUrl2) {
const alwaysAuth2 = core.getInput('always-auth2');
auth.configAuthentication2(registryUrl2, alwaysAuth2);
}
}
if (cache && (0, cache_utils_1.isCacheFeatureAvailable)()) {
core.saveState(constants_1.State.CachePackageManager, cache);

View file

@ -5,33 +5,29 @@ import * as core from '@actions/core';
import * as github from '@actions/github';
export function configAuthentication(registryUrl: string, alwaysAuth: string) {
const npmrc: string = path.resolve(
writeRegistryToFile(registryUrl, alwaysAuth, 'NODE_AUTH_TOKEN');
}
function getFileLocation(): string {
return path.resolve(
process.env['RUNNER_TEMP'] || process.cwd(),
'.npmrc'
);
if (!registryUrl.endsWith('/')) {
registryUrl += '/';
}
writeRegistryToFile(registryUrl, npmrc, alwaysAuth);
}
function writeRegistryToFile(
registryUrl: string,
fileLocation: string,
alwaysAuth: string
alwaysAuth: string,
tokenEnvVar: string
) {
let scope: string = core.getInput('scope');
if (!scope && registryUrl.indexOf('npm.pkg.github.com') > -1) {
scope = github.context.repo.owner;
}
if (scope && scope[0] != '@') {
scope = '@' + scope;
}
if (scope) {
scope = scope.toLowerCase() + ':';
const fileLocation: string = getFileLocation();
if (!registryUrl.endsWith('/')) {
registryUrl += '/';
}
const scope: string = getScope('scope', registryUrl);
core.debug(`Setting auth in ${fileLocation}`);
let newContents = '';
if (fs.existsSync(fileLocation)) {
@ -45,7 +41,7 @@ function writeRegistryToFile(
}
// Remove http: or https: from front of registry.
const authString: string =
registryUrl.replace(/(^\w+:|^)/, '') + ':_authToken=${NODE_AUTH_TOKEN}';
registryUrl.replace(/(^\w+:|^)/, '') + ':_authToken=${' + tokenEnvVar + '}';
const registryString = `${scope}registry=${registryUrl}`;
const alwaysAuthString = `always-auth=${alwaysAuth}`;
newContents += `${authString}${os.EOL}${registryString}${os.EOL}${alwaysAuthString}`;
@ -53,7 +49,24 @@ function writeRegistryToFile(
core.exportVariable('NPM_CONFIG_USERCONFIG', fileLocation);
// Export empty node_auth_token if didn't exist so npm doesn't complain about not being able to find it
core.exportVariable(
'NODE_AUTH_TOKEN',
process.env.NODE_AUTH_TOKEN || 'XXXXX-XXXXX-XXXXX-XXXXX'
tokenEnvVar, process.env[tokenEnvVar] || 'XXXXX-XXXXX-XXXXX-XXXXX'
);
}
function getScope(scopeKey: string, registryUrl: string) {
let scope: string = core.getInput(scopeKey);
if (!scope && registryUrl.indexOf('npm.pkg.github.com') > -1) {
scope = github.context.repo.owner;
}
if (scope && scope[0] != '@') {
scope = '@' + scope;
}
if (scope) {
scope = scope.toLowerCase() + ':';
}
return scope;
}
export function configAuthentication2(registryUrl2: string, alwaysAuth2: string) {
writeRegistryToFile(registryUrl2, alwaysAuth2, 'NODE_AUTH_TOKEN2');
}

View file

@ -58,6 +58,11 @@ export async function run() {
const alwaysAuth: string = core.getInput('always-auth');
if (registryUrl) {
auth.configAuthentication(registryUrl, alwaysAuth);
const registryUrl2: string = core.getInput('registry-url2');
if (registryUrl2) {
const alwaysAuth2: string = core.getInput('always-auth2');
auth.configAuthentication2(registryUrl2, alwaysAuth2);
}
}
if (cache && isCacheFeatureAvailable()) {