2022-12-02 11:14:43 +01:00
|
|
|
import * as cache from "@actions/cache";
|
|
|
|
import * as core from "@actions/core";
|
|
|
|
|
2022-12-05 12:36:14 +01:00
|
|
|
import { Events, Inputs, Outputs, State } from "./constants";
|
|
|
|
import { IOutputSetter } from "./outputSetter";
|
2022-12-02 11:14:43 +01:00
|
|
|
import * as utils from "./utils/actionUtils";
|
|
|
|
|
2022-12-05 12:36:14 +01:00
|
|
|
async function run(outputter: IOutputSetter): Promise<string | undefined> {
|
2022-12-02 11:14:43 +01:00
|
|
|
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-05 12:36:14 +01:00
|
|
|
outputter.setState(State.CachePrimaryKey, primaryKey);
|
2022-12-02 11:14:43 +01:00
|
|
|
|
|
|
|
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 12:36:14 +01:00
|
|
|
// Store the matched cache key in states
|
|
|
|
//utils.setCacheState(cacheKey);
|
|
|
|
outputter.setState(State.CacheMatchedKey, cacheKey);
|
|
|
|
|
|
|
|
const isExactKeyMatch = utils.isExactKeyMatch(
|
|
|
|
core.getInput(Inputs.Key, { required: true }),
|
|
|
|
cacheKey
|
|
|
|
);
|
|
|
|
//utils.setCacheHitOutput(isExactKeyMatch);
|
|
|
|
outputter.setOutput(Outputs.CacheHit, isExactKeyMatch.toString());
|
|
|
|
core.info(`Cache restored from key: ${cacheKey}`);
|
|
|
|
|
2022-12-02 11:14:43 +01:00
|
|
|
return cacheKey;
|
|
|
|
} catch (error: unknown) {
|
|
|
|
core.setFailed((error as Error).message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default run;
|