mirror of https://github.com/actions/setup-go.git
Add logic for modules caching
Such files as cache-restore.ts, cache-utils.ts, constants.ts, cache-save.ts we added. Main.ts file now incorporates logic for using files mentioned above.pull/228/head
parent
5ba3482d38
commit
25a133c257
@ -0,0 +1,66 @@
|
|||||||
|
import * as cache from '@actions/cache';
|
||||||
|
import * as core from '@actions/core';
|
||||||
|
import * as glob from '@actions/glob';
|
||||||
|
import path from 'path';
|
||||||
|
import fs from 'fs';
|
||||||
|
|
||||||
|
import {State, Outputs} from './constants';
|
||||||
|
import {
|
||||||
|
getCacheDirectoryPath,
|
||||||
|
getPackageManagerInfo,
|
||||||
|
PackageManagerInfo
|
||||||
|
} from './cache-utils';
|
||||||
|
|
||||||
|
export const restoreCache = async (
|
||||||
|
packageManager: string,
|
||||||
|
cacheDependencyPath?: string
|
||||||
|
) => {
|
||||||
|
const packageManagerInfo = await getPackageManagerInfo();
|
||||||
|
const platform = process.env.RUNNER_OS;
|
||||||
|
|
||||||
|
const cachePath = await getCacheDirectoryPath(
|
||||||
|
packageManagerInfo
|
||||||
|
);
|
||||||
|
|
||||||
|
const goSumFilePath = cacheDependencyPath
|
||||||
|
? cacheDependencyPath
|
||||||
|
: findGoSumFile(packageManagerInfo);
|
||||||
|
const fileHash = await glob.hashFiles(goSumFilePath);
|
||||||
|
|
||||||
|
if (!fileHash) {
|
||||||
|
throw new Error(
|
||||||
|
'Some specified paths were not resolved, unable to cache dependencies.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const primaryKey = `go-cache-${platform}-${fileHash}`;
|
||||||
|
core.debug(`primary key is ${primaryKey}`);
|
||||||
|
|
||||||
|
core.saveState(State.CachePrimaryKey, primaryKey);
|
||||||
|
|
||||||
|
const cacheKey = await cache.restoreCache([cachePath], primaryKey);
|
||||||
|
core.setOutput('cache-hit', Boolean(cacheKey));
|
||||||
|
|
||||||
|
if (!cacheKey) {
|
||||||
|
core.info(`${packageManager} cache is not found`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
core.saveState(State.CacheMatchedKey, cacheKey);
|
||||||
|
core.info(`Cache restored from key: ${cacheKey}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
const findGoSumFile = (packageManager: PackageManagerInfo) => {
|
||||||
|
let goSumFile = packageManager.goSumFilePattern;
|
||||||
|
const workspace = process.env.GITHUB_WORKSPACE!;
|
||||||
|
const rootContent = fs.readdirSync(workspace);
|
||||||
|
|
||||||
|
const goSumFileExists = rootContent.includes(goSumFile);
|
||||||
|
if (!goSumFileExists) {
|
||||||
|
throw new Error(
|
||||||
|
`Dependencies file go.sum is not found in ${workspace}. Supported file pattern: ${goSumFile}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return path.join(workspace, goSumFile);
|
||||||
|
};
|
@ -0,0 +1,60 @@
|
|||||||
|
import * as core from '@actions/core';
|
||||||
|
import * as cache from '@actions/cache';
|
||||||
|
import fs from 'fs';
|
||||||
|
import {State} from './constants';
|
||||||
|
import {getCacheDirectoryPath, getPackageManagerInfo} from './cache-utils';
|
||||||
|
|
||||||
|
// Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in
|
||||||
|
// @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to
|
||||||
|
// throw an uncaught exception. Instead of failing this action, just warn.
|
||||||
|
process.on('uncaughtException', e => {
|
||||||
|
const warningPrefix = '[warning]';
|
||||||
|
core.info(`${warningPrefix}${e.message}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
export async function run() {
|
||||||
|
try {
|
||||||
|
await cachePackages();
|
||||||
|
} catch (error) {
|
||||||
|
core.setFailed(error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const cachePackages = async () => {
|
||||||
|
const state = core.getState(State.CacheMatchedKey);
|
||||||
|
const primaryKey = core.getState(State.CachePrimaryKey);
|
||||||
|
|
||||||
|
const packageManagerInfo = await getPackageManagerInfo();
|
||||||
|
|
||||||
|
const cachePath = await getCacheDirectoryPath(
|
||||||
|
packageManagerInfo,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!fs.existsSync(cachePath)) {
|
||||||
|
throw new Error(
|
||||||
|
`Cache folder path is retrieved but doesn't exist on disk: ${cachePath}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (primaryKey === state) {
|
||||||
|
core.info(
|
||||||
|
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await cache.saveCache([cachePath], primaryKey);
|
||||||
|
core.info(`Cache saved with the key: ${primaryKey}`);
|
||||||
|
} catch (error) {
|
||||||
|
if (error.name === cache.ValidationError.name) {
|
||||||
|
throw error;
|
||||||
|
} else if (error.name === cache.ReserveCacheError.name) {
|
||||||
|
core.info(error.message);
|
||||||
|
} else {
|
||||||
|
core.warning(`${error.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
run();
|
@ -0,0 +1,49 @@
|
|||||||
|
import * as core from '@actions/core';
|
||||||
|
import * as exec from '@actions/exec';
|
||||||
|
|
||||||
|
export interface PackageManagerInfo {
|
||||||
|
goSumFilePattern: string;
|
||||||
|
getCacheFolderCommand: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const defaultPackageManager: PackageManagerInfo = {
|
||||||
|
goSumFilePattern: 'go.sum',
|
||||||
|
getCacheFolderCommand: 'go env GOMODCACHE',
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getCommandOutput = async (toolCommand: string) => {
|
||||||
|
let {stdout, stderr, exitCode} = await exec.getExecOutput(
|
||||||
|
toolCommand,
|
||||||
|
undefined,
|
||||||
|
{ignoreReturnCode: true}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (exitCode) {
|
||||||
|
stderr = !stderr.trim()
|
||||||
|
? `The '${toolCommand}' command failed with exit code: ${exitCode}`
|
||||||
|
: stderr;
|
||||||
|
throw new Error(stderr);
|
||||||
|
}
|
||||||
|
|
||||||
|
return stdout.trim();
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getPackageManagerInfo = async () => {
|
||||||
|
|
||||||
|
return defaultPackageManager;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getCacheDirectoryPath = async (
|
||||||
|
packageManagerInfo: PackageManagerInfo,
|
||||||
|
) => {
|
||||||
|
const stdout = await getCommandOutput(
|
||||||
|
packageManagerInfo.getCacheFolderCommand
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!stdout) {
|
||||||
|
throw new Error(`Could not get cache folder path.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return stdout;
|
||||||
|
};
|
@ -0,0 +1,8 @@
|
|||||||
|
export enum State {
|
||||||
|
CachePrimaryKey = 'CACHE_KEY',
|
||||||
|
CacheMatchedKey = 'CACHE_RESULT'
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum Outputs {
|
||||||
|
CacheHit = 'cache-hit'
|
||||||
|
}
|
Loading…
Reference in New Issue