support ghes

This commit is contained in:
eric sciple 2020-05-15 14:43:02 -04:00
parent 2ae9264901
commit e37249e03d
5 changed files with 481 additions and 72 deletions

View file

@ -6,7 +6,6 @@ import * as io from '@actions/io';
import * as tc from '@actions/tool-cache';
import * as path from 'path';
import * as semver from 'semver';
import {Url} from 'url';
import fs = require('fs');
//
@ -20,7 +19,6 @@ export interface INodeVersion {
interface INodeVersionInfo {
downloadUrl: string;
token: string | null;
resolvedVersion: string;
fileName: string;
}
@ -28,13 +26,12 @@ interface INodeVersionInfo {
export async function getNode(
versionSpec: string,
stable: boolean,
token: string
auth: string | undefined
) {
let osPlat: string = os.platform();
let osArch: string = translateArchToDistUrl(os.arch());
// check cache
let info: INodeVersionInfo | null = null;
let toolPath: string;
toolPath = tc.find('node', versionSpec);
@ -43,32 +40,61 @@ export async function getNode(
console.log(`Found in cache @ ${toolPath}`);
} else {
console.log(`Attempting to download ${versionSpec}...`);
let info = await getInfoFromManifest(versionSpec, stable, token);
if (!info) {
console.log(
'Not found in manifest. Falling back to download directly from Node'
);
info = await getInfoFromDist(versionSpec);
}
if (!info) {
throw new Error(
`Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`
);
}
console.log(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`);
let downloadPath = '';
let info: INodeVersionInfo | null = null;
//
// Try download from internal distribution (popular versions only)
//
try {
downloadPath = await tc.downloadTool(info.downloadUrl, undefined, token);
info = await getInfoFromManifest(versionSpec, stable, auth);
if (info) {
console.log(
`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`
);
downloadPath = await tc.downloadTool(info.downloadUrl, undefined, auth);
} else {
console.log(
'Not found in manifest. Falling back to download directly from Node'
);
}
} catch (err) {
if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
await acquireNodeFromFallbackLocation(info.resolvedVersion);
return;
// Rate limit?
if (
err instanceof tc.HTTPError &&
(err.httpStatusCode === 403 || err.httpStatusCode === 429)
) {
console.log(
`Received HTTP status code ${err.httpStatusCode}. This usually indicates the rate limit has been exceeded`
);
} else {
console.log(err.message);
}
core.debug(err.stack);
console.log('Falling back to download directly from Node');
}
//
// Download from nodejs.org
//
if (!downloadPath) {
info = await getInfoFromDist(versionSpec);
if (!info) {
throw new Error(
`Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`
);
}
throw err;
console.log(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`);
try {
downloadPath = await tc.downloadTool(info.downloadUrl);
} catch (err) {
if (err instanceof tc.HTTPError && err.httpStatusCode == 404) {
return await acquireNodeFromFallbackLocation(info.resolvedVersion);
}
throw err;
}
}
//
@ -76,6 +102,7 @@ export async function getNode(
//
console.log('Extracting ...');
let extPath: string;
info = info || ({} as INodeVersionInfo); // satisfy compiler, never null when reaches here
if (osPlat == 'win32') {
let _7zPath = path.join(__dirname, '..', 'externals', '7zr.exe');
extPath = await tc.extract7z(downloadPath, undefined, _7zPath);
@ -117,13 +144,13 @@ export async function getNode(
async function getInfoFromManifest(
versionSpec: string,
stable: boolean,
token: string
auth: string | undefined
): Promise<INodeVersionInfo | null> {
let info: INodeVersionInfo | null = null;
const releases = await tc.getManifestFromRepo(
'actions',
'node-versions',
token
auth
);
console.log(`matching ${versionSpec}...`);
const rel = await tc.findFromManifest(versionSpec, stable, releases);
@ -133,7 +160,6 @@ async function getInfoFromManifest(
info.resolvedVersion = rel.version;
info.downloadUrl = rel.files[0].download_url;
info.fileName = rel.files[0].filename;
info.token = token;
}
return info;
@ -145,7 +171,6 @@ async function getInfoFromDist(
let osPlat: string = os.platform();
let osArch: string = translateArchToDistUrl(os.arch());
let info: INodeVersionInfo | null = null;
let version: string;
version = await queryDistForMatch(versionSpec);

View file

@ -2,6 +2,7 @@ import * as core from '@actions/core';
import * as installer from './installer';
import * as auth from './authutil';
import * as path from 'path';
import {URL} from 'url';
export async function run() {
try {
@ -17,8 +18,9 @@ export async function run() {
console.log(`version: ${version}`);
if (version) {
let token = core.getInput('token');
let auth = !token || isGhes() ? undefined : `token ${token}`;
let stable = (core.getInput('stable') || 'true').toUpperCase() === 'TRUE';
await installer.getNode(version, stable, token);
await installer.getNode(version, stable, auth);
}
const registryUrl: string = core.getInput('registry-url');
@ -39,3 +41,10 @@ export async function run() {
core.setFailed(error.message);
}
}
function isGhes(): boolean {
const ghUrl = new URL(
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
);
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}