1
0
Fork 0
mirror of https://github.com/actions/cache.git synced 2025-07-15 09:20:46 +00:00

Add update-env-variable to force/disable cache update.

This fixes https://github.com/actions/cache/issues/342
This commit is contained in:
eyal0 2022-09-10 16:43:05 -06:00
parent b195c997a4
commit 3d41dc5e6b
7 changed files with 136 additions and 10 deletions

View file

@ -2,7 +2,8 @@ export enum Inputs {
Key = "key",
Path = "path",
RestoreKeys = "restore-keys",
UploadChunkSize = "upload-chunk-size"
UploadChunkSize = "upload-chunk-size",
UpdateEnvVariable = "update-env-variable"
}
export enum Outputs {

View file

@ -33,11 +33,36 @@ async function run(): Promise<void> {
return;
}
if (utils.isExactKeyMatch(primaryKey, state)) {
core.info(
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`
);
return;
const envVarName = core.getInput(Inputs.UpdateEnvVariable);
let forceUpdate;
if (envVarName) {
let envVarValue;
envVarValue = process.env[envVarName];
if (["true"].includes(envVarValue.toLowerCase())) {
forcedUpdate = true;
} else if (["false"].includes(envVarValue.toLowerCase())) {
forcedUpdate = false;
}
}
if (forcedUpdate !== undefined) {
if (forceUpdate) {
core.info(
`Cache saving was forced by setting "${envVarName}" to "${envVarValue}".`
);
} else {
core.info(
`Cache saving was disabled by setting "${envVarName}" to "${envVarValue}".`
);
return;
}
} else {
core.info(`"${envVarName}" is not set.`);
if (utils.isExactKeyMatch(primaryKey, state)) {
core.info(
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`
);
return;
}
}
const cachePaths = utils.getInputAsArray(Inputs.Path, {

View file

@ -13,11 +13,14 @@ interface CacheInput {
path: string;
key: string;
restoreKeys?: string[];
updateEnvVariable?: string;
}
export function setInputs(input: CacheInput): void {
setInput(Inputs.Path, input.path);
setInput(Inputs.Key, input.key);
input.updateEnvVariable &&
setInput(Inputs.UpdateEnvVariable, input.updateEnvVariable);
input.restoreKeys &&
setInput(Inputs.RestoreKeys, input.restoreKeys.join("\n"));
}
@ -25,6 +28,7 @@ export function setInputs(input: CacheInput): void {
export function clearInputs(): void {
delete process.env[getInputName(Inputs.Path)];
delete process.env[getInputName(Inputs.Key)];
delete process.env[getInputName(Inputs.UpdateEnvVariable)];
delete process.env[getInputName(Inputs.RestoreKeys)];
delete process.env[getInputName(Inputs.UploadChunkSize)];
}