2021-11-17 10:31:22 +00:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as cache from '@actions/cache';
|
|
|
|
|
|
|
|
import fs from 'fs';
|
|
|
|
import {State} from './cache-distributions/cache-distributor';
|
|
|
|
|
2023-12-05 13:52:09 +00:00
|
|
|
// Added early exit to resolve issue with slow post action step:
|
|
|
|
// - https://github.com/actions/setup-node/issues/878
|
|
|
|
// https://github.com/actions/cache/pull/1217
|
|
|
|
export async function run(earlyExit?: boolean) {
|
2021-11-17 10:31:22 +00:00
|
|
|
try {
|
|
|
|
const cache = core.getInput('cache');
|
|
|
|
if (cache) {
|
|
|
|
await saveCache(cache);
|
2023-12-05 13:52:09 +00:00
|
|
|
|
|
|
|
if (earlyExit) {
|
|
|
|
process.exit(0);
|
|
|
|
}
|
2021-11-17 10:31:22 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
const err = error as Error;
|
|
|
|
core.setFailed(err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function saveCache(packageManager: string) {
|
2023-04-06 10:02:34 +00:00
|
|
|
const cachePathState = core.getState(State.CACHE_PATHS);
|
|
|
|
|
|
|
|
if (!cachePathState) {
|
|
|
|
core.warning(
|
|
|
|
'Cache paths are empty. Please check the previous logs and make sure that the python version is specified'
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const cachePaths = JSON.parse(cachePathState) as string[];
|
2021-11-17 10:31:22 +00:00
|
|
|
|
|
|
|
core.debug(`paths for caching are ${cachePaths.join(', ')}`);
|
|
|
|
|
|
|
|
if (!isCacheDirectoryExists(cachePaths)) {
|
|
|
|
throw new Error(
|
|
|
|
`Cache folder path is retrieved for ${packageManager} but doesn't exist on disk: ${cachePaths.join(
|
|
|
|
', '
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const primaryKey = core.getState(State.STATE_CACHE_PRIMARY_KEY);
|
|
|
|
const matchedKey = core.getState(State.CACHE_MATCHED_KEY);
|
|
|
|
|
|
|
|
if (!primaryKey) {
|
|
|
|
core.warning('Error retrieving key from state.');
|
|
|
|
return;
|
|
|
|
} else if (matchedKey === primaryKey) {
|
|
|
|
// no change in target directories
|
|
|
|
core.info(
|
|
|
|
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-10 11:15:18 +00:00
|
|
|
let cacheId = 0;
|
|
|
|
|
|
|
|
try {
|
|
|
|
cacheId = await cache.saveCache(cachePaths, primaryKey);
|
|
|
|
} catch (err) {
|
|
|
|
const message = (err as Error).message;
|
|
|
|
core.info(`[warning]${message}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-28 13:17:50 +00:00
|
|
|
if (cacheId == -1) {
|
|
|
|
return;
|
2021-11-17 10:31:22 +00:00
|
|
|
}
|
2022-06-28 13:17:50 +00:00
|
|
|
core.info(`Cache saved with the key: ${primaryKey}`);
|
2021-11-17 10:31:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isCacheDirectoryExists(cacheDirectory: string[]) {
|
|
|
|
const result = cacheDirectory.reduce((previousValue, currentValue) => {
|
|
|
|
return previousValue || fs.existsSync(currentValue);
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-12-05 13:52:09 +00:00
|
|
|
run(true);
|