From 0d93be3424692a737cd2a7333be007dcef9a45ea Mon Sep 17 00:00:00 2001 From: PrinsFrank <25006490+PrinsFrank@users.noreply.github.com> Date: Fri, 2 Aug 2024 21:12:44 +0200 Subject: [PATCH] Also add "force-overwrite" option to save-only action --- save/action.yml | 4 ++++ src/saveImpl.ts | 10 ++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/save/action.yml b/save/action.yml index 6696d45..2ab5387 100644 --- a/save/action.yml +++ b/save/action.yml @@ -15,6 +15,10 @@ inputs: description: 'An optional boolean when enabled, allows windows runners to save caches that can be restored on other platforms' default: '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 runs: using: 'node20' main: '../dist/save-only/index.js' diff --git a/src/saveImpl.ts b/src/saveImpl.ts index 2013b23..8291bc2 100644 --- a/src/saveImpl.ts +++ b/src/saveImpl.ts @@ -8,7 +8,6 @@ import { StateProvider } from "./stateProvider"; 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 // @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to @@ -51,13 +50,16 @@ export async function saveImpl( const forceOverwrite = utils.getInputAsBool(Inputs.ForceOverwrite); if (utils.isExactKeyMatch(primaryKey, restoredKey) && !forceOverwrite) { core.info( - `Cache hit occurred on the primary key ${primaryKey} and force-overwrite is disabled, not saving cache.` + `Cache hit occurred on the primary key "${primaryKey}" and force-overwrite is disabled, not saving cache.` ); return; } - if (utils.isExactKeyMatch(primaryKey, restoredKey) && forceOverwrite) { - await issueCommand('actions-cache delete', {}, primaryKey); + if ((restoredKey == undefined || utils.isExactKeyMatch(primaryKey, restoredKey)) && forceOverwrite) { + core.info( + `Cache hit occurred on the primary key "${primaryKey}" or running as save-only and force-overwrite is enabled, deleting cache.` + ); + await cache.deleteCache(primaryKey) } const cachePaths = utils.getInputAsArray(Inputs.Path, {