mirror of
https://github.com/actions/setup-go.git
synced 2025-06-29 00:13:44 +00: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
60
src/cache-save.ts
Normal file
60
src/cache-save.ts
Normal file
|
@ -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();
|
Loading…
Add table
Add a link
Reference in a new issue