mirror of
https://github.com/actions/setup-go.git
synced 2025-07-16 20:15:14 -06:00
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.
This commit is contained in:
parent
5ba3482d38
commit
25a133c257
7 changed files with 198 additions and 2 deletions
66
src/cache-restore.ts
Normal file
66
src/cache-restore.ts
Normal file
|
@ -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);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue