mirror of
https://github.com/actions/setup-go.git
synced 2025-06-30 05:03:43 +00:00
Merge branch 'modules-caching' into modules-caching-tests
This commit is contained in:
commit
1a1c412f7e
17 changed files with 212 additions and 453 deletions
|
@ -21,10 +21,13 @@ export async function run() {
|
|||
}
|
||||
|
||||
const cachePackages = async () => {
|
||||
const cachingFlag = core.getBooleanInput('cache');
|
||||
if (!cachingFlag) return;
|
||||
const cacheInput = core.getInput('cache');
|
||||
if (!cacheInput) {
|
||||
return;
|
||||
}
|
||||
|
||||
const packageManager = core.getInput('package-manager');
|
||||
const packageManager =
|
||||
cacheInput.toUpperCase() === 'TRUE' ? 'default' : cacheInput;
|
||||
|
||||
const state = core.getState(State.CacheMatchedKey);
|
||||
const primaryKey = core.getState(State.CachePrimaryKey);
|
||||
|
|
|
@ -25,8 +25,9 @@ export const getPackageManagerInfo = async (packageManager: string) => {
|
|||
`It's not possible to use ${packageManager}, please, check correctness of the package manager name spelling.`
|
||||
);
|
||||
}
|
||||
const obtainedPackageManager = supportedPackageManagers[packageManager];
|
||||
|
||||
return supportedPackageManagers[packageManager];
|
||||
return obtainedPackageManager;
|
||||
};
|
||||
|
||||
export const getCacheDirectoryPath = async (
|
||||
|
|
|
@ -30,7 +30,6 @@ export interface IGoVersionInfo {
|
|||
|
||||
export async function getGo(
|
||||
versionSpec: string,
|
||||
stable: boolean,
|
||||
checkLatest: boolean,
|
||||
auth: string | undefined
|
||||
) {
|
||||
|
@ -41,7 +40,7 @@ export async function getGo(
|
|||
core.info('Attempting to resolve the latest version from the manifest...');
|
||||
const resolvedVersion = await resolveVersionFromManifest(
|
||||
versionSpec,
|
||||
stable,
|
||||
true,
|
||||
auth
|
||||
);
|
||||
if (resolvedVersion) {
|
||||
|
@ -68,7 +67,7 @@ export async function getGo(
|
|||
// Try download from internal distribution (popular versions only)
|
||||
//
|
||||
try {
|
||||
info = await getInfoFromManifest(versionSpec, stable, auth);
|
||||
info = await getInfoFromManifest(versionSpec, true, auth);
|
||||
if (info) {
|
||||
downloadPath = await installGoVersion(info, auth);
|
||||
} else {
|
||||
|
@ -95,7 +94,7 @@ export async function getGo(
|
|||
// Download from storage.googleapis.com
|
||||
//
|
||||
if (!downloadPath) {
|
||||
info = await getInfoFromDist(versionSpec, stable);
|
||||
info = await getInfoFromDist(versionSpec);
|
||||
if (!info) {
|
||||
throw new Error(
|
||||
`Unable to find Go version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`
|
||||
|
@ -191,11 +190,10 @@ export async function getInfoFromManifest(
|
|||
}
|
||||
|
||||
async function getInfoFromDist(
|
||||
versionSpec: string,
|
||||
stable: boolean
|
||||
versionSpec: string
|
||||
): Promise<IGoVersionInfo | null> {
|
||||
let version: IGoVersion | undefined;
|
||||
version = await findMatch(versionSpec, stable);
|
||||
version = await findMatch(versionSpec);
|
||||
if (!version) {
|
||||
return null;
|
||||
}
|
||||
|
@ -211,8 +209,7 @@ async function getInfoFromDist(
|
|||
}
|
||||
|
||||
export async function findMatch(
|
||||
versionSpec: string,
|
||||
stable: boolean
|
||||
versionSpec: string
|
||||
): Promise<IGoVersion | undefined> {
|
||||
let archFilter = sys.getArch();
|
||||
let platFilter = sys.getPlatform();
|
||||
|
@ -233,18 +230,8 @@ export async function findMatch(
|
|||
let candidate: IGoVersion = candidates[i];
|
||||
let version = makeSemver(candidate.version);
|
||||
|
||||
// 1.13.0 is advertised as 1.13 preventing being able to match exactly 1.13.0
|
||||
// since a semver of 1.13 would match latest 1.13
|
||||
let parts: string[] = version.split('.');
|
||||
if (parts.length == 2) {
|
||||
version = version + '.0';
|
||||
}
|
||||
|
||||
core.debug(`check ${version} satisfies ${versionSpec}`);
|
||||
if (
|
||||
semver.satisfies(version, versionSpec) &&
|
||||
(!stable || candidate.stable === stable)
|
||||
) {
|
||||
if (semver.satisfies(version, versionSpec)) {
|
||||
goFile = candidate.files.find(file => {
|
||||
core.debug(
|
||||
`${file.arch}===${archFilter} && ${file.os}===${platFilter}`
|
||||
|
@ -284,20 +271,30 @@ export async function getVersionsDist(
|
|||
// Convert the go version syntax into semver for semver matching
|
||||
// 1.13.1 => 1.13.1
|
||||
// 1.13 => 1.13.0
|
||||
// 1.10beta1 => 1.10.0-beta1, 1.10rc1 => 1.10.0-rc1
|
||||
// 1.8.5beta1 => 1.8.5-beta1, 1.8.5rc1 => 1.8.5-rc1
|
||||
// 1.10beta1 => 1.10.0-beta.1, 1.10rc1 => 1.10.0-rc.1
|
||||
// 1.8.5beta1 => 1.8.5-beta.1, 1.8.5rc1 => 1.8.5-rc.1
|
||||
export function makeSemver(version: string): string {
|
||||
version = version.replace('go', '');
|
||||
version = version.replace('beta', '-beta').replace('rc', '-rc');
|
||||
version = version.replace('beta', '-beta.').replace('rc', '-rc.');
|
||||
let parts = version.split('-');
|
||||
|
||||
let verPart: string = parts[0];
|
||||
let prereleasePart = parts.length > 1 ? `-${parts[1]}` : '';
|
||||
|
||||
let verParts: string[] = verPart.split('.');
|
||||
if (verParts.length == 2) {
|
||||
verPart += '.0';
|
||||
let semVersion = semver.coerce(parts[0])?.version;
|
||||
if (!semVersion) {
|
||||
throw new Error(
|
||||
`The version: ${version} can't be changed to SemVer notation`
|
||||
);
|
||||
}
|
||||
|
||||
return `${verPart}${prereleasePart}`;
|
||||
if (!parts[1]) {
|
||||
return semVersion;
|
||||
}
|
||||
|
||||
const fullVersion = semver.valid(`${semVersion}-${parts[1]}`);
|
||||
|
||||
if (!fullVersion) {
|
||||
throw new Error(
|
||||
`The version: ${version} can't be changed to SemVer notation`
|
||||
);
|
||||
}
|
||||
return fullVersion;
|
||||
}
|
||||
|
|
30
src/main.ts
30
src/main.ts
|
@ -1,6 +1,7 @@
|
|||
import * as core from '@actions/core';
|
||||
import * as io from '@actions/io';
|
||||
import * as installer from './installer';
|
||||
import * as semver from 'semver';
|
||||
import path from 'path';
|
||||
import {restoreCache} from './cache-restore';
|
||||
import cp from 'child_process';
|
||||
|
@ -15,29 +16,26 @@ export async function run() {
|
|||
//
|
||||
let versionSpec = core.getInput('go-version');
|
||||
|
||||
// stable will be true unless false is the exact input
|
||||
// since getting unstable versions should be explicit
|
||||
let stable = (core.getInput('stable') || 'true').toUpperCase() === 'TRUE';
|
||||
const cache = core.getBooleanInput('cache');
|
||||
|
||||
core.info(`Setup go ${stable ? 'stable' : ''} version spec ${versionSpec}`);
|
||||
const cache = core.getInput('cache');
|
||||
core.info(`Setup go version spec ${versionSpec}`);
|
||||
|
||||
if (versionSpec) {
|
||||
let token = core.getInput('token');
|
||||
let auth = !token || isGhes() ? undefined : `token ${token}`;
|
||||
|
||||
const checkLatest = core.getBooleanInput('check-latest');
|
||||
const installDir = await installer.getGo(
|
||||
versionSpec,
|
||||
stable,
|
||||
checkLatest,
|
||||
auth
|
||||
);
|
||||
const installDir = await installer.getGo(versionSpec, checkLatest, auth);
|
||||
|
||||
core.exportVariable('GOROOT', installDir);
|
||||
core.addPath(path.join(installDir, 'bin'));
|
||||
core.info('Added go to the path');
|
||||
|
||||
const version = installer.makeSemver(versionSpec);
|
||||
// Go versions less than 1.9 require GOROOT to be set
|
||||
if (semver.lt(version, '1.9.0')) {
|
||||
core.info('Setting GOROOT for Go version < 1.9');
|
||||
core.exportVariable('GOROOT', installDir);
|
||||
}
|
||||
|
||||
let added = await addBinToPath();
|
||||
core.debug(`add bin ${added}`);
|
||||
core.info(`Successfully setup go version ${versionSpec}`);
|
||||
|
@ -47,7 +45,7 @@ export async function run() {
|
|||
if (isGhes()) {
|
||||
throw new Error('Caching is not supported on GHES');
|
||||
}
|
||||
const packageManager = core.getInput('package-manager');
|
||||
const packageManager = cache.toUpperCase() === 'TRUE' ? 'default' : cache;
|
||||
const cacheDependencyPath = core.getInput('cache-dependency-path');
|
||||
await restoreCache(packageManager, cacheDependencyPath);
|
||||
}
|
||||
|
@ -86,13 +84,13 @@ export async function addBinToPath(): Promise<boolean> {
|
|||
if (!fs.existsSync(gp)) {
|
||||
// some of the hosted images have go install but not profile dir
|
||||
core.debug(`creating ${gp}`);
|
||||
io.mkdirP(gp);
|
||||
await io.mkdirP(gp);
|
||||
}
|
||||
|
||||
let bp = path.join(gp, 'bin');
|
||||
if (!fs.existsSync(bp)) {
|
||||
core.debug(`creating ${bp}`);
|
||||
io.mkdirP(bp);
|
||||
await io.mkdirP(bp);
|
||||
}
|
||||
|
||||
core.addPath(bp);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
let os = require('os');
|
||||
const os = require('os');
|
||||
|
||||
export function getPlatform(): string {
|
||||
// darwin and linux match already
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue