2022-12-21 22:08:44 +08:00
|
|
|
import * as cache from "@actions/cache";
|
|
|
|
import * as core from "@actions/core";
|
|
|
|
|
|
|
|
import { Events, Inputs, Outputs, State } from "./constants";
|
2023-08-09 22:36:51 +08:00
|
|
|
import {
|
|
|
|
IStateProvider,
|
|
|
|
NullStateProvider,
|
|
|
|
StateProvider
|
|
|
|
} from "./stateProvider";
|
2022-12-21 22:08:44 +08:00
|
|
|
import * as utils from "./utils/actionUtils";
|
|
|
|
|
2023-08-09 22:36:51 +08:00
|
|
|
export async function restoreImpl(
|
2024-02-16 04:58:25 +08:00
|
|
|
stateProvider: IStateProvider,
|
|
|
|
earlyExit?: boolean | undefined
|
2022-12-21 22:08:44 +08:00
|
|
|
): Promise<string | undefined> {
|
|
|
|
try {
|
|
|
|
if (!utils.isCacheFeatureAvailable()) {
|
|
|
|
core.setOutput(Outputs.CacheHit, "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 });
|
|
|
|
stateProvider.setState(State.CachePrimaryKey, primaryKey);
|
|
|
|
|
|
|
|
const restoreKeys = utils.getInputAsArray(Inputs.RestoreKeys);
|
|
|
|
const cachePaths = utils.getInputAsArray(Inputs.Path, {
|
|
|
|
required: true
|
|
|
|
});
|
2023-01-05 19:19:13 +08:00
|
|
|
const enableCrossOsArchive = utils.getInputAsBool(
|
|
|
|
Inputs.EnableCrossOsArchive
|
|
|
|
);
|
2023-01-30 19:10:58 +08:00
|
|
|
const failOnCacheMiss = utils.getInputAsBool(Inputs.FailOnCacheMiss);
|
2023-03-09 20:30:28 +08:00
|
|
|
const lookupOnly = utils.getInputAsBool(Inputs.LookupOnly);
|
2022-12-21 22:08:44 +08:00
|
|
|
|
|
|
|
const cacheKey = await cache.restoreCache(
|
|
|
|
cachePaths,
|
|
|
|
primaryKey,
|
2023-01-05 19:19:13 +08:00
|
|
|
restoreKeys,
|
2023-03-09 20:30:28 +08:00
|
|
|
{ lookupOnly: lookupOnly },
|
2023-01-05 19:19:13 +08:00
|
|
|
enableCrossOsArchive
|
2022-12-21 22:08:44 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!cacheKey) {
|
2023-01-30 19:10:58 +08:00
|
|
|
if (failOnCacheMiss) {
|
|
|
|
throw new Error(
|
|
|
|
`Failed to restore cache entry. Exiting as fail-on-cache-miss is set. Input key: ${primaryKey}`
|
|
|
|
);
|
|
|
|
}
|
2022-12-21 22:08:44 +08:00
|
|
|
core.info(
|
|
|
|
`Cache not found for input keys: ${[
|
|
|
|
primaryKey,
|
|
|
|
...restoreKeys
|
|
|
|
].join(", ")}`
|
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the matched cache key in states
|
|
|
|
stateProvider.setState(State.CacheMatchedKey, cacheKey);
|
|
|
|
|
|
|
|
const isExactKeyMatch = utils.isExactKeyMatch(
|
|
|
|
core.getInput(Inputs.Key, { required: true }),
|
|
|
|
cacheKey
|
|
|
|
);
|
|
|
|
|
|
|
|
core.setOutput(Outputs.CacheHit, isExactKeyMatch.toString());
|
2023-03-09 20:30:28 +08:00
|
|
|
if (lookupOnly) {
|
|
|
|
core.info(`Cache found and can be restored from key: ${cacheKey}`);
|
|
|
|
} else {
|
|
|
|
core.info(`Cache restored from key: ${cacheKey}`);
|
|
|
|
}
|
2022-12-21 22:08:44 +08:00
|
|
|
|
|
|
|
return cacheKey;
|
|
|
|
} catch (error: unknown) {
|
|
|
|
core.setFailed((error as Error).message);
|
2024-02-16 04:58:25 +08:00
|
|
|
if (earlyExit) {
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2022-12-21 22:08:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-09 22:36:51 +08:00
|
|
|
async function run(
|
|
|
|
stateProvider: IStateProvider,
|
|
|
|
earlyExit: boolean | undefined
|
|
|
|
): Promise<void> {
|
2024-02-16 04:58:25 +08:00
|
|
|
await restoreImpl(stateProvider, earlyExit);
|
2023-08-09 22:36:51 +08:00
|
|
|
|
|
|
|
// node will stay alive if any promises are not resolved,
|
|
|
|
// which is a possibility if HTTP requests are dangling
|
|
|
|
// due to retries or timeouts. We know that if we got here
|
|
|
|
// that all promises that we care about have successfully
|
|
|
|
// resolved, so simply exit with success.
|
|
|
|
if (earlyExit) {
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function restoreOnlyRun(
|
|
|
|
earlyExit?: boolean | undefined
|
|
|
|
): Promise<void> {
|
|
|
|
await run(new NullStateProvider(), earlyExit);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function restoreRun(
|
|
|
|
earlyExit?: boolean | undefined
|
|
|
|
): Promise<void> {
|
|
|
|
await run(new StateProvider(), earlyExit);
|
|
|
|
}
|