2022-11-29 05:56:53 -05:00
|
|
|
import * as cache from "@actions/cache";
|
|
|
|
import * as core from "@actions/core";
|
2020-03-18 09:35:13 -04:00
|
|
|
|
2022-11-29 05:56:53 -05:00
|
|
|
import { Events, Inputs, State, Variables } from "./constants";
|
|
|
|
import * as utils from "./utils/actionUtils";
|
|
|
|
|
|
|
|
async function run(): Promise<void> {
|
|
|
|
try {
|
|
|
|
if (!utils.isCacheFeatureAvailable()) {
|
|
|
|
utils.setCacheHitOutput(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const primaryKey = core.getInput(Inputs.Key, { required: true });
|
|
|
|
core.saveState(State.CachePrimaryKey, primaryKey);
|
|
|
|
|
|
|
|
const restoreKeys = utils.getInputAsArray(Inputs.RestoreKeys);
|
|
|
|
const cachePaths = utils.getInputAsArray(Inputs.Path, {
|
|
|
|
required: true
|
|
|
|
});
|
|
|
|
|
|
|
|
const cacheKey = await cache.restoreCache(
|
|
|
|
cachePaths,
|
|
|
|
primaryKey,
|
|
|
|
restoreKeys
|
|
|
|
);
|
|
|
|
|
|
|
|
//Check if user wants to save cache despite of failure in any previous job
|
2022-11-30 05:47:09 -05:00
|
|
|
const saveCache = core.getInput(Inputs.SaveOnAnyFailure).toLowerCase();
|
|
|
|
if (saveCache == "true") {
|
2022-11-30 03:11:55 -05:00
|
|
|
core.debug(
|
|
|
|
`Exporting environment variable ${Variables.SaveCacheOnAnyFailure}`
|
|
|
|
);
|
2022-11-29 05:56:53 -05:00
|
|
|
core.exportVariable(Variables.SaveCacheOnAnyFailure, saveCache);
|
|
|
|
core.info(
|
2022-11-30 03:11:55 -05:00
|
|
|
`Input Variable ${Variables.SaveCacheOnAnyFailure} is set to true, the cache will be saved despite of any failure in the build.`
|
2022-11-29 05:56:53 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cacheKey) {
|
2022-11-30 05:47:09 -05:00
|
|
|
if (core.getInput(Inputs.FailOnCacheMiss).toLowerCase() == "true") {
|
2022-11-29 05:56:53 -05:00
|
|
|
throw new Error(
|
2022-11-30 03:26:50 -05:00
|
|
|
`Cache with the given input key ${primaryKey} is not found, hence exiting the workflow as the fail-on-cache-miss requirement is not met.`
|
2022-11-29 05:56:53 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
core.info(
|
|
|
|
`Cache not found for input keys: ${[
|
|
|
|
primaryKey,
|
|
|
|
...restoreKeys
|
|
|
|
].join(", ")}`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Store the matched cache key
|
|
|
|
utils.setCacheState(cacheKey);
|
|
|
|
|
|
|
|
const isExactKeyMatch = utils.isExactKeyMatch(primaryKey, cacheKey);
|
|
|
|
utils.setCacheHitOutput(isExactKeyMatch);
|
|
|
|
|
2022-11-30 03:11:55 -05:00
|
|
|
if (
|
|
|
|
!isExactKeyMatch &&
|
2022-11-30 03:26:50 -05:00
|
|
|
core.getBooleanInput(Inputs.FailOnCacheMiss) == true
|
2022-11-30 03:11:55 -05:00
|
|
|
) {
|
2022-11-29 05:56:53 -05:00
|
|
|
throw new Error(
|
2022-11-30 03:26:50 -05:00
|
|
|
`Restored cache key doesn't match the given input key ${primaryKey}, hence exiting the workflow as the fail-on-cache-miss requirement is not met.`
|
2022-11-29 05:56:53 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
core.info(`Cache restored from key: ${cacheKey}`);
|
|
|
|
} catch (error: unknown) {
|
|
|
|
core.setFailed((error as Error).message);
|
|
|
|
}
|
2019-10-30 14:48:49 -04:00
|
|
|
}
|
|
|
|
|
2022-11-29 05:56:53 -05:00
|
|
|
run();
|
|
|
|
|
|
|
|
export default run;
|