mirror of
https://github.com/actions/setup-go.git
synced 2025-05-22 02:22:24 +00:00
Use GitHub releases to download Go versions. (#58)
This commit is contained in:
215
src/installer.ts
215
src/installer.ts
@ -1,46 +1,12 @@
|
||||
import * as tc from '@actions/tool-cache';
|
||||
import * as core from '@actions/core';
|
||||
import * as path from 'path';
|
||||
import * as semver from 'semver';
|
||||
import * as httpm from '@actions/http-client';
|
||||
import * as sys from './system';
|
||||
import {debug} from '@actions/core';
|
||||
import os from 'os';
|
||||
|
||||
export async function downloadGo(
|
||||
versionSpec: string,
|
||||
stable: boolean
|
||||
): Promise<string | undefined> {
|
||||
let toolPath: string | undefined;
|
||||
|
||||
try {
|
||||
let match: IGoVersion | undefined = await findMatch(versionSpec, stable);
|
||||
|
||||
if (match) {
|
||||
// download
|
||||
debug(`match ${match.version}`);
|
||||
let downloadUrl: string = `https://storage.googleapis.com/golang/${match.files[0].filename}`;
|
||||
console.log(`Downloading from ${downloadUrl}`);
|
||||
|
||||
let downloadPath: string = await tc.downloadTool(downloadUrl);
|
||||
debug(`downloaded to ${downloadPath}`);
|
||||
|
||||
// extract
|
||||
console.log('Extracting ...');
|
||||
let extPath: string =
|
||||
sys.getPlatform() == 'windows'
|
||||
? await tc.extractZip(downloadPath)
|
||||
: await tc.extractTar(downloadPath);
|
||||
debug(`extracted to ${extPath}`);
|
||||
|
||||
// extracts with a root folder that matches the fileName downloaded
|
||||
const toolRoot = path.join(extPath, 'go');
|
||||
toolPath = await tc.cacheDir(toolRoot, 'go', makeSemver(match.version));
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to download version ${versionSpec}: ${error}`);
|
||||
}
|
||||
|
||||
return toolPath;
|
||||
}
|
||||
type InstallationType = 'dist' | 'manifest';
|
||||
|
||||
export interface IGoVersionFile {
|
||||
filename: string;
|
||||
@ -55,6 +21,160 @@ export interface IGoVersion {
|
||||
files: IGoVersionFile[];
|
||||
}
|
||||
|
||||
export interface IGoVersionInfo {
|
||||
type: InstallationType;
|
||||
downloadUrl: string;
|
||||
resolvedVersion: string;
|
||||
fileName: string;
|
||||
}
|
||||
|
||||
export async function getGo(
|
||||
versionSpec: string,
|
||||
stable: boolean,
|
||||
auth: string | undefined
|
||||
) {
|
||||
let osPlat: string = os.platform();
|
||||
let osArch: string = os.arch();
|
||||
|
||||
// check cache
|
||||
let toolPath: string;
|
||||
toolPath = tc.find('go', versionSpec);
|
||||
// If not found in cache, download
|
||||
if (toolPath) {
|
||||
core.info(`Found in cache @ ${toolPath}`);
|
||||
return toolPath;
|
||||
}
|
||||
core.info(`Attempting to download ${versionSpec}...`);
|
||||
let downloadPath = '';
|
||||
let info: IGoVersionInfo | null = null;
|
||||
|
||||
//
|
||||
// Try download from internal distribution (popular versions only)
|
||||
//
|
||||
try {
|
||||
info = await getInfoFromManifest(versionSpec, stable, auth);
|
||||
if (info) {
|
||||
downloadPath = await installGoVersion(info, auth);
|
||||
} else {
|
||||
core.info(
|
||||
'Not found in manifest. Falling back to download directly from Go'
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
if (
|
||||
err instanceof tc.HTTPError &&
|
||||
(err.httpStatusCode === 403 || err.httpStatusCode === 429)
|
||||
) {
|
||||
core.info(
|
||||
`Received HTTP status code ${err.httpStatusCode}. This usually indicates the rate limit has been exceeded`
|
||||
);
|
||||
} else {
|
||||
core.info(err.message);
|
||||
}
|
||||
core.debug(err.stack);
|
||||
core.info('Falling back to download directly from Go');
|
||||
}
|
||||
|
||||
//
|
||||
// Download from storage.googleapis.com
|
||||
//
|
||||
if (!downloadPath) {
|
||||
info = await getInfoFromDist(versionSpec, stable);
|
||||
if (!info) {
|
||||
throw new Error(
|
||||
`Unable to find Go version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
core.info('Install from dist');
|
||||
downloadPath = await installGoVersion(info, undefined);
|
||||
} catch (err) {
|
||||
throw new Error(`Failed to download version ${versionSpec}: ${err}`);
|
||||
}
|
||||
}
|
||||
|
||||
return downloadPath;
|
||||
}
|
||||
|
||||
async function installGoVersion(
|
||||
info: IGoVersionInfo,
|
||||
auth: string | undefined
|
||||
): Promise<string> {
|
||||
core.info(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`);
|
||||
const downloadPath = await tc.downloadTool(info.downloadUrl, undefined, auth);
|
||||
|
||||
core.info('Extracting Go...');
|
||||
let extPath = await extractGoArchive(downloadPath);
|
||||
core.info(`Successfully extracted go to ${extPath}`);
|
||||
if (info.type === 'dist') {
|
||||
extPath = path.join(extPath, 'go');
|
||||
}
|
||||
|
||||
core.info('Adding to the cache ...');
|
||||
const cachedDir = await tc.cacheDir(
|
||||
extPath,
|
||||
'go',
|
||||
makeSemver(info.resolvedVersion)
|
||||
);
|
||||
core.info(`Successfully cached go to ${cachedDir}`);
|
||||
return cachedDir;
|
||||
}
|
||||
|
||||
export async function extractGoArchive(archivePath: string): Promise<string> {
|
||||
const arch = os.arch();
|
||||
let extPath: string;
|
||||
|
||||
if (arch === 'win32') {
|
||||
extPath = await tc.extractZip(archivePath);
|
||||
} else {
|
||||
extPath = await tc.extractTar(archivePath);
|
||||
}
|
||||
|
||||
return extPath;
|
||||
}
|
||||
|
||||
export async function getInfoFromManifest(
|
||||
versionSpec: string,
|
||||
stable: boolean,
|
||||
auth: string | undefined
|
||||
): Promise<IGoVersionInfo | null> {
|
||||
let info: IGoVersionInfo | null = null;
|
||||
const releases = await tc.getManifestFromRepo('actions', 'go-versions', auth);
|
||||
core.info(`matching ${versionSpec}...`);
|
||||
const rel = await tc.findFromManifest(versionSpec, stable, releases);
|
||||
|
||||
if (rel && rel.files.length > 0) {
|
||||
info = <IGoVersionInfo>{};
|
||||
info.type = 'manifest';
|
||||
info.resolvedVersion = rel.version;
|
||||
info.downloadUrl = rel.files[0].download_url;
|
||||
info.fileName = rel.files[0].filename;
|
||||
}
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
async function getInfoFromDist(
|
||||
versionSpec: string,
|
||||
stable: boolean
|
||||
): Promise<IGoVersionInfo | null> {
|
||||
let version: IGoVersion | undefined;
|
||||
version = await findMatch(versionSpec, stable);
|
||||
if (!version) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let downloadUrl: string = `https://storage.googleapis.com/golang/${version.files[0].filename}`;
|
||||
|
||||
return <IGoVersionInfo>{
|
||||
type: 'dist',
|
||||
downloadUrl: downloadUrl,
|
||||
resolvedVersion: version.version,
|
||||
fileName: version.files[0].filename
|
||||
};
|
||||
}
|
||||
|
||||
export async function findMatch(
|
||||
versionSpec: string,
|
||||
stable: boolean
|
||||
@ -66,7 +186,9 @@ export async function findMatch(
|
||||
let match: IGoVersion | undefined;
|
||||
|
||||
const dlUrl: string = 'https://golang.org/dl/?mode=json&include=all';
|
||||
let candidates: IGoVersion[] | null = await module.exports.getVersions(dlUrl);
|
||||
let candidates: IGoVersion[] | null = await module.exports.getVersionsDist(
|
||||
dlUrl
|
||||
);
|
||||
if (!candidates) {
|
||||
throw new Error(`golang download url did not return results`);
|
||||
}
|
||||
@ -83,18 +205,20 @@ export async function findMatch(
|
||||
version = version + '.0';
|
||||
}
|
||||
|
||||
debug(`check ${version} satisfies ${versionSpec}`);
|
||||
core.debug(`check ${version} satisfies ${versionSpec}`);
|
||||
if (
|
||||
semver.satisfies(version, versionSpec) &&
|
||||
(!stable || candidate.stable === stable)
|
||||
) {
|
||||
goFile = candidate.files.find(file => {
|
||||
debug(`${file.arch}===${archFilter} && ${file.os}===${platFilter}`);
|
||||
core.debug(
|
||||
`${file.arch}===${archFilter} && ${file.os}===${platFilter}`
|
||||
);
|
||||
return file.arch === archFilter && file.os === platFilter;
|
||||
});
|
||||
|
||||
if (goFile) {
|
||||
debug(`matched ${candidate.version}`);
|
||||
core.debug(`matched ${candidate.version}`);
|
||||
match = candidate;
|
||||
break;
|
||||
}
|
||||
@ -110,9 +234,14 @@ export async function findMatch(
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function getVersions(dlUrl: string): Promise<IGoVersion[] | null> {
|
||||
export async function getVersionsDist(
|
||||
dlUrl: string
|
||||
): Promise<IGoVersion[] | null> {
|
||||
// this returns versions descending so latest is first
|
||||
let http: httpm.HttpClient = new httpm.HttpClient('setup-go');
|
||||
let http: httpm.HttpClient = new httpm.HttpClient('setup-go', [], {
|
||||
allowRedirects: true,
|
||||
maxRedirects: 3
|
||||
});
|
||||
return (await http.getJson<IGoVersion[]>(dlUrl)).result;
|
||||
}
|
||||
|
||||
|
57
src/main.ts
57
src/main.ts
@ -1,10 +1,10 @@
|
||||
import * as core from '@actions/core';
|
||||
import * as io from '@actions/io';
|
||||
import * as tc from '@actions/tool-cache';
|
||||
import * as installer from './installer';
|
||||
import path from 'path';
|
||||
import cp from 'child_process';
|
||||
import fs from 'fs';
|
||||
import {URL} from 'url';
|
||||
|
||||
export async function run() {
|
||||
try {
|
||||
@ -18,47 +18,35 @@ export async function run() {
|
||||
// since getting unstable versions should be explicit
|
||||
let stable = (core.getInput('stable') || 'true').toUpperCase() === 'TRUE';
|
||||
|
||||
console.log(
|
||||
`Setup go ${stable ? 'stable' : ''} version spec ${versionSpec}`
|
||||
);
|
||||
core.info(`Setup go ${stable ? 'stable' : ''} version spec ${versionSpec}`);
|
||||
|
||||
if (versionSpec) {
|
||||
let installDir: string | undefined = tc.find('go', versionSpec);
|
||||
let token = core.getInput('token');
|
||||
let auth = !token || isGhes() ? undefined : `token ${token}`;
|
||||
|
||||
if (!installDir) {
|
||||
console.log(
|
||||
`A version satisfying ${versionSpec} not found locally, attempting to download ...`
|
||||
);
|
||||
installDir = await installer.downloadGo(versionSpec, stable);
|
||||
console.log('Installed');
|
||||
}
|
||||
const installDir = await installer.getGo(versionSpec, stable, auth);
|
||||
|
||||
if (installDir) {
|
||||
core.exportVariable('GOROOT', installDir);
|
||||
core.addPath(path.join(installDir, 'bin'));
|
||||
console.log('Added go to the path');
|
||||
core.exportVariable('GOROOT', installDir);
|
||||
core.addPath(path.join(installDir, 'bin'));
|
||||
core.info('Added go to the path');
|
||||
|
||||
let added = addBinToPath();
|
||||
core.debug(`add bin ${added}`);
|
||||
} else {
|
||||
throw new Error(
|
||||
`Could not find a version that satisfied version spec: ${versionSpec}`
|
||||
);
|
||||
}
|
||||
let added = await addBinToPath();
|
||||
core.debug(`add bin ${added}`);
|
||||
core.info(`Successfully setup go version ${versionSpec}`);
|
||||
}
|
||||
|
||||
// add problem matchers
|
||||
const matchersPath = path.join(__dirname, '..', 'matchers.json');
|
||||
console.log(`##[add-matcher]${matchersPath}`);
|
||||
core.info(`##[add-matcher]${matchersPath}`);
|
||||
|
||||
// output the version actually being used
|
||||
let goPath = await io.which('go');
|
||||
let goVersion = (cp.execSync(`${goPath} version`) || '').toString();
|
||||
console.log(goVersion);
|
||||
core.info(goVersion);
|
||||
|
||||
core.startGroup('go env');
|
||||
let goEnv = (cp.execSync(`${goPath} env`) || '').toString();
|
||||
console.log(goEnv);
|
||||
core.info(goEnv);
|
||||
core.endGroup();
|
||||
} catch (error) {
|
||||
core.setFailed(error.message);
|
||||
@ -68,25 +56,25 @@ export async function run() {
|
||||
export async function addBinToPath(): Promise<boolean> {
|
||||
let added = false;
|
||||
let g = await io.which('go');
|
||||
_debug(`which go :${g}:`);
|
||||
core.debug(`which go :${g}:`);
|
||||
if (!g) {
|
||||
_debug('go not in the path');
|
||||
core.debug('go not in the path');
|
||||
return added;
|
||||
}
|
||||
|
||||
let buf = cp.execSync('go env GOPATH');
|
||||
if (buf) {
|
||||
let gp = buf.toString().trim();
|
||||
_debug(`go env GOPATH :${gp}:`);
|
||||
core.debug(`go env GOPATH :${gp}:`);
|
||||
if (!fs.existsSync(gp)) {
|
||||
// some of the hosted images have go install but not profile dir
|
||||
_debug(`creating ${gp}`);
|
||||
core.debug(`creating ${gp}`);
|
||||
io.mkdirP(gp);
|
||||
}
|
||||
|
||||
let bp = path.join(gp, 'bin');
|
||||
if (!fs.existsSync(bp)) {
|
||||
_debug(`creating ${bp}`);
|
||||
core.debug(`creating ${bp}`);
|
||||
io.mkdirP(bp);
|
||||
}
|
||||
|
||||
@ -96,6 +84,9 @@ export async function addBinToPath(): Promise<boolean> {
|
||||
return added;
|
||||
}
|
||||
|
||||
export function _debug(message: string) {
|
||||
core.debug(message);
|
||||
function isGhes(): boolean {
|
||||
const ghUrl = new URL(
|
||||
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
|
||||
);
|
||||
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
|
||||
}
|
||||
|
Reference in New Issue
Block a user