mirror of
https://code.forgejo.org/actions/cache.git
synced 2025-04-21 16:23:56 +08:00
Reverted custom asks implemented and added wrapper
This commit is contained in:
parent
4b5f33df54
commit
11ab7ccfa2
10 changed files with 242 additions and 337 deletions
|
@ -2,13 +2,13 @@ export enum Inputs {
|
|||
Key = "key",
|
||||
Path = "path",
|
||||
RestoreKeys = "restore-keys",
|
||||
UploadChunkSize = "upload-chunk-size",
|
||||
FailOnCacheMiss = "fail-on-cache-miss",
|
||||
SaveOnAnyFailure = "save-on-any-failure"
|
||||
UploadChunkSize = "upload-chunk-size"
|
||||
}
|
||||
|
||||
export enum Outputs {
|
||||
CacheHit = "cache-hit"
|
||||
CacheHit = "cache-hit",
|
||||
Key = "key",
|
||||
Path = "path"
|
||||
}
|
||||
|
||||
export enum State {
|
||||
|
@ -22,8 +22,4 @@ export enum Events {
|
|||
PullRequest = "pull_request"
|
||||
}
|
||||
|
||||
export enum Variables {
|
||||
SaveCacheOnAnyFailure = "SAVE_CACHE_ON_ANY_FAILURE"
|
||||
}
|
||||
|
||||
export const RefKey = "GITHUB_REF";
|
||||
|
|
|
@ -1,86 +1,22 @@
|
|||
import * as cache from "@actions/cache";
|
||||
import * as core from "@actions/core";
|
||||
|
||||
import { Events, Inputs, State, Variables } from "./constants";
|
||||
import { Inputs } from "./constants";
|
||||
import run from "./restoreImpl";
|
||||
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
|
||||
const saveCache = core.getInput(Inputs.SaveOnAnyFailure).toLowerCase();
|
||||
if (saveCache == "true") {
|
||||
core.debug(
|
||||
`Exporting environment variable ${Variables.SaveCacheOnAnyFailure}`
|
||||
);
|
||||
core.exportVariable(Variables.SaveCacheOnAnyFailure, saveCache);
|
||||
core.info(
|
||||
`Input Variable ${Variables.SaveCacheOnAnyFailure} is set to true, the cache will be saved despite of any failure in the build.`
|
||||
);
|
||||
}
|
||||
|
||||
if (!cacheKey) {
|
||||
if (core.getInput(Inputs.FailOnCacheMiss).toLowerCase() == "true") {
|
||||
throw new Error(
|
||||
`Cache with the given input key ${primaryKey} is not found, hence exiting the workflow as the fail-on-cache-miss requirement is not met.`
|
||||
);
|
||||
}
|
||||
core.info(
|
||||
`Cache not found for input keys: ${[
|
||||
primaryKey,
|
||||
...restoreKeys
|
||||
].join(", ")}`
|
||||
);
|
||||
return;
|
||||
}
|
||||
// Store the matched cache key
|
||||
async function restore(): Promise<void> {
|
||||
const cacheKey = await run();
|
||||
if (cacheKey) {
|
||||
// Store the matched cache key in states
|
||||
utils.setCacheState(cacheKey);
|
||||
|
||||
const isExactKeyMatch = utils.isExactKeyMatch(primaryKey, cacheKey);
|
||||
const isExactKeyMatch = utils.isExactKeyMatch(
|
||||
core.getInput(Inputs.Key, { required: true }),
|
||||
cacheKey
|
||||
);
|
||||
utils.setCacheHitOutput(isExactKeyMatch);
|
||||
|
||||
if (
|
||||
!isExactKeyMatch &&
|
||||
core.getBooleanInput(Inputs.FailOnCacheMiss) == true
|
||||
) {
|
||||
throw new Error(
|
||||
`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.`
|
||||
);
|
||||
}
|
||||
core.info(`Cache restored from key: ${cacheKey}`);
|
||||
} catch (error: unknown) {
|
||||
core.setFailed((error as Error).message);
|
||||
}
|
||||
}
|
||||
|
||||
run();
|
||||
|
||||
export default run;
|
||||
export default restore;
|
||||
|
|
55
src/restoreImpl.ts
Normal file
55
src/restoreImpl.ts
Normal file
|
@ -0,0 +1,55 @@
|
|||
import * as cache from "@actions/cache";
|
||||
import * as core from "@actions/core";
|
||||
|
||||
import { Events, Inputs, State } from "./constants";
|
||||
import * as utils from "./utils/actionUtils";
|
||||
|
||||
async function run(): 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 });
|
||||
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
|
||||
);
|
||||
|
||||
if (!cacheKey) {
|
||||
core.info(
|
||||
`Cache not found for input keys: ${[
|
||||
primaryKey,
|
||||
...restoreKeys
|
||||
].join(", ")}`
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
return cacheKey;
|
||||
} catch (error: unknown) {
|
||||
core.setFailed((error as Error).message);
|
||||
}
|
||||
}
|
||||
|
||||
export default run;
|
17
src/restoreOnly.ts
Normal file
17
src/restoreOnly.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
import * as core from "@actions/core";
|
||||
|
||||
import { Outputs } from "./constants";
|
||||
import run from "./restoreImpl";
|
||||
import * as utils from "./utils/actionUtils";
|
||||
|
||||
async function restoreOnly(): Promise<void> {
|
||||
const cacheKey = await run();
|
||||
if (cacheKey) {
|
||||
// Store the matched cache key in output
|
||||
core.setOutput(Outputs.Key, utils.getCacheState());
|
||||
|
||||
core.info(`Cache restored from key: ${cacheKey}`);
|
||||
}
|
||||
}
|
||||
|
||||
export default restoreOnly;
|
|
@ -13,28 +13,18 @@ interface CacheInput {
|
|||
path: string;
|
||||
key: string;
|
||||
restoreKeys?: string[];
|
||||
failOnCacheMiss?: boolean;
|
||||
saveOnAnyFailure?: boolean;
|
||||
}
|
||||
|
||||
export function setInputs(input: CacheInput): void {
|
||||
setInput(Inputs.Path, input.path);
|
||||
setInput(Inputs.Key, input.key);
|
||||
setInput(Inputs.SaveOnAnyFailure, "false");
|
||||
setInput(Inputs.FailOnCacheMiss, "false");
|
||||
input.restoreKeys &&
|
||||
setInput(Inputs.RestoreKeys, input.restoreKeys.join("\n"));
|
||||
input.failOnCacheMiss &&
|
||||
setInput(Inputs.FailOnCacheMiss, String(input.failOnCacheMiss));
|
||||
input.saveOnAnyFailure &&
|
||||
setInput(Inputs.SaveOnAnyFailure, String(input.saveOnAnyFailure));
|
||||
}
|
||||
|
||||
export function clearInputs(): void {
|
||||
delete process.env[getInputName(Inputs.Path)];
|
||||
delete process.env[getInputName(Inputs.Key)];
|
||||
delete process.env[getInputName(Inputs.RestoreKeys)];
|
||||
delete process.env[getInputName(Inputs.FailOnCacheMiss)];
|
||||
delete process.env[getInputName(Inputs.SaveOnAnyFailure)];
|
||||
delete process.env[getInputName(Inputs.UploadChunkSize)];
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue