cache/src/restoreImpl.ts

78 lines
2.1 KiB
TypeScript
Raw Normal View History

import * as cache from "@actions/cache";
import * as core from "@actions/core";
2022-12-05 11:36:14 +00:00
import { Events, Inputs, Outputs, State } from "./constants";
2022-12-06 18:26:58 +00:00
import { IStateProvider } from "./stateProvider";
import * as utils from "./utils/actionUtils";
2022-12-08 08:24:43 +00:00
async function restoreImpl(
stateProvider: IStateProvider
): Promise<string | undefined> {
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 });
2022-12-09 13:56:33 +00:00
stateProvider.setState(
State.CachePrimaryKey,
primaryKey,
Outputs.InputtedKey
);
const restoreKeys = utils.getInputAsArray(Inputs.RestoreKeys);
const cachePaths = utils.getInputAsArray(Inputs.Path, {
required: true
});
const cacheKey = await cache.restoreCache(
cachePaths,
primaryKey,
restoreKeys
);
if (!cacheKey) {
core.info(
`Cache not found for input keys: ${[
primaryKey,
...restoreKeys
].join(", ")}`
);
return;
}
2022-12-05 11:36:14 +00:00
// Store the matched cache key in states
2022-12-09 13:56:33 +00:00
stateProvider.setState(
State.CacheMatchedKey,
cacheKey,
Outputs.MatchedKey
);
2022-12-05 11:36:14 +00:00
const isExactKeyMatch = utils.isExactKeyMatch(
core.getInput(Inputs.Key, { required: true }),
cacheKey
);
2022-12-06 18:26:58 +00:00
core.setOutput(Outputs.CacheHit, isExactKeyMatch.toString());
2022-12-05 11:36:14 +00:00
core.info(`Cache restored from key: ${cacheKey}`);
return cacheKey;
} catch (error: unknown) {
core.setFailed((error as Error).message);
}
}
2022-12-06 18:26:58 +00:00
export default restoreImpl;