Add force-overwrite for cache

This commit is contained in:
Frank Prins 2024-01-22 18:54:37 +01:00
parent a2ed59d39b
commit 7dd9af18b0
4 changed files with 19 additions and 4 deletions

View File

@ -14,6 +14,10 @@ See ["Caching dependencies to speed up workflows"](https://docs.github.com/en/ac
## What's New ## What's New
### Unreleased
* Added the `force-overwrite` flag to force overwrite any existing cache for incremental caching
### v4 ### v4
* Updated to node 20 * Updated to node 20

View File

@ -30,6 +30,10 @@ inputs:
description: 'Run the post step to save the cache even if another step before fails' description: 'Run the post step to save the cache even if another step before fails'
default: 'false' default: 'false'
required: false required: false
force-overwrite:
description: 'Delete any previous (incremental) cache before pushing a new cache even if a cache for the primary cache key exists'
default: 'false'
required: false
outputs: outputs:
cache-hit: cache-hit:
description: 'A boolean value to indicate an exact match was found for the primary key' description: 'A boolean value to indicate an exact match was found for the primary key'

View File

@ -5,7 +5,8 @@ export enum Inputs {
UploadChunkSize = "upload-chunk-size", // Input for cache, save action UploadChunkSize = "upload-chunk-size", // Input for cache, save action
EnableCrossOsArchive = "enableCrossOsArchive", // Input for cache, restore, save action EnableCrossOsArchive = "enableCrossOsArchive", // Input for cache, restore, save action
FailOnCacheMiss = "fail-on-cache-miss", // Input for cache, restore action FailOnCacheMiss = "fail-on-cache-miss", // Input for cache, restore action
LookupOnly = "lookup-only" // Input for cache, restore action LookupOnly = "lookup-only", // Input for cache, restore action
ForceOverwrite = "force-overwrite" // Input for cache action
} }
export enum Outputs { export enum Outputs {

View File

@ -8,6 +8,7 @@ import {
StateProvider StateProvider
} from "./stateProvider"; } from "./stateProvider";
import * as utils from "./utils/actionUtils"; import * as utils from "./utils/actionUtils";
import { issueCommand } from "@actions/core/lib/command";
// Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in // Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in
// @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to // @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to
@ -47,13 +48,18 @@ export async function saveImpl(
// NO-OP in case of SaveOnly action // NO-OP in case of SaveOnly action
const restoredKey = stateProvider.getCacheState(); const restoredKey = stateProvider.getCacheState();
if (utils.isExactKeyMatch(primaryKey, restoredKey)) { const forceOverwrite = utils.getInputAsBool(Inputs.ForceOverwrite);
if (utils.isExactKeyMatch(primaryKey, restoredKey) && !forceOverwrite) {
core.info( core.info(
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.` `Cache hit occurred on the primary key ${primaryKey} and force-overwrite is disabled, not saving cache.`
); );
return; return;
} }
if (utils.isExactKeyMatch(primaryKey, restoredKey) && forceOverwrite) {
await issueCommand('actions-cache delete', {}, primaryKey);
}
const cachePaths = utils.getInputAsArray(Inputs.Path, { const cachePaths = utils.getInputAsArray(Inputs.Path, {
required: true required: true
}); });