You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cache/src/restore.ts

80 lines
2.8 KiB
TypeScript

import * as cache from "@actions/cache";
5 years ago
import * as core from "@actions/core";
import { Events, Inputs, State, Variables } from "./constants";
5 years ago
import * as utils from "./utils/actionUtils";
async function run(): Promise<void> {
5 years ago
try {
if (!utils.isCacheFeatureAvailable()) {
4 years ago
utils.setCacheHitOutput(false);
return;
}
5 years ago
// Validate inputs, this can cause task failure
if (!utils.isValidEvent()) {
utils.logWarning(
`Event Validation Error: The event type ${
process.env[Events.Key]
} is not supported because it's not tied to a branch or tag ref.`
);
return;
}
5 years ago
const primaryKey = core.getInput(Inputs.Key, { required: true });
core.saveState(State.CachePrimaryKey, primaryKey);
5 years ago
const restoreKeys = utils.getInputAsArray(Inputs.RestoreKeys);
const cachePaths = utils.getInputAsArray(Inputs.Path, {
required: true
});
5 years ago
const cacheKey = await cache.restoreCache(
cachePaths,
primaryKey,
restoreKeys
);
//Check if user wants to save cache despite of failure in any previous job
const saveCache = core.getInput(Inputs.SaveOnAnyFailure);
if (saveCache === "yes") {
core.debug(`save cache input variable is set to yes`);
core.exportVariable(Variables.SaveCacheOnAnyFailure, saveCache);
core.info(
`Input Variable ${Variables.SaveCacheOnAnyFailure} is set to yes, the cache will be saved despite of any failure in the build.`
);
}
if (!cacheKey) {
if (core.getInput(Inputs.StrictRestore) == "yes") {
throw new Error(
`Cache with the given input key ${primaryKey} is not found, hence exiting the workflow as the strict-restore requirement is not met.`
);
}
core.info(
`Cache not found for input keys: ${[
primaryKey,
...restoreKeys
].join(", ")}`
5 years ago
);
return;
}
// Store the matched cache key
utils.setCacheState(cacheKey);
5 years ago
const isExactKeyMatch = utils.isExactKeyMatch(primaryKey, cacheKey);
utils.setCacheHitOutput(isExactKeyMatch);
if (!isExactKeyMatch && core.getInput(Inputs.StrictRestore) == "yes") {
throw new Error(
`Restored cache key doesn't match the given input key ${primaryKey}, hence exiting the workflow as the strict-restore requirement is not met.`
);
}
core.info(`Cache restored from key: ${cacheKey}`);
} catch (error: unknown) {
3 years ago
core.setFailed((error as Error).message);
5 years ago
}
}
export default run;