From 4ae7258cec9a7e5d62d2d2e9b5d86bc75187372f Mon Sep 17 00:00:00 2001 From: Ferran Basora Date: Thu, 1 Dec 2022 14:53:15 +0100 Subject: [PATCH] Fix save logic --- dist/restore/index.js | 6 +++--- dist/save/index.js | 16 ++++++++++------ src/restore.ts | 6 +++--- src/save.ts | 19 +++++++++++++------ 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/dist/restore/index.js b/dist/restore/index.js index 8d418ee..f44a204 100644 --- a/dist/restore/index.js +++ b/dist/restore/index.js @@ -103396,12 +103396,12 @@ function restoreCache() { const localKey = path.join(local, key, cacheFileName); core.info(`Looking for exact match: ${localKey}`); if (fs.existsSync(localKey)) { - core.info('Local cache HIT!'); + core.info('Local cache HIT! ✅'); yield fs.copy(localKey, archivePath); core.info('Local cache copied!'); core.info('Extracting cache file...'); yield tar_1.extractTar(archivePath, compressionMethod); - utils_1.saveMatchedKey(localKey); + utils_1.saveMatchedKey(key); utils_1.setCacheHitOutput(true); utils_1.setCacheHitLocal(true); core.info("Cache restored from local successfully"); @@ -103409,7 +103409,7 @@ function restoreCache() { } else { utils_1.setCacheHitLocal(false); - core.info('Local cache MISS!'); + core.info('Local cache MISS! ❌'); } } const mc = utils_1.newMinio(); diff --git a/dist/save/index.js b/dist/save/index.js index aafa298..990ad7f 100644 --- a/dist/save/index.js +++ b/dist/save/index.js @@ -98659,7 +98659,9 @@ function saveCache() { const useFallback = utils_1.getInputAsBoolean("use-fallback"); const paths = utils_1.getInputAsArray("path"); const local = core.getInput("local"); - if (((local && utils_1.isLocalHit()) || !local) && utils_1.isExactKeyMatch()) { + const shouldNotStoreInLocal = !local || (local && utils_1.isLocalHit()); + const shouldNotStoreInRemote = utils_1.isExactKeyMatch(); + if (shouldNotStoreInLocal && shouldNotStoreInRemote) { core.info("Cache was exact key match, not saving"); return; } @@ -98682,11 +98684,13 @@ function saveCache() { if (core.isDebug()) { yield tar_1.listTar(archivePath, compressionMethod); } - const object = path.join(key, cacheFileName); - core.info(`Uploading tar to s3. Bucket: ${bucket}, Object: ${object}`); - yield mc.fPutObject(bucket, object, archivePath, {}); - core.info("Cache saved to s3 successfully"); - if (local) { + if (!shouldNotStoreInRemote) { + const object = path.join(key, cacheFileName); + core.info(`Uploading tar to s3. Bucket: ${bucket}, Object: ${object}`); + yield mc.fPutObject(bucket, object, archivePath, {}); + core.info("Cache saved to s3 successfully"); + } + if (!shouldNotStoreInLocal) { core.info("Local cache is enabled"); const localKey = path.join(local, key, cacheFileName); core.info(`Storing local cache to: ${localKey}`); diff --git a/src/restore.ts b/src/restore.ts index 1555949..75f3ba8 100644 --- a/src/restore.ts +++ b/src/restore.ts @@ -50,14 +50,14 @@ async function restoreCache() { core.info(`Looking for exact match: ${localKey}`); if (fs.existsSync(localKey)) { - core.info('Local cache HIT!') + core.info('Local cache HIT! ✅') await fs.copy(localKey, archivePath) core.info('Local cache copied!') core.info('Extracting cache file...') await extractTar(archivePath, compressionMethod); - saveMatchedKey(localKey); + saveMatchedKey(key); setCacheHitOutput(true); setCacheHitLocal(true); @@ -65,7 +65,7 @@ async function restoreCache() { return } else { setCacheHitLocal(false); - core.info('Local cache MISS!') + core.info('Local cache MISS! ❌') } } diff --git a/src/save.ts b/src/save.ts index 71cbaa4..13f49ac 100644 --- a/src/save.ts +++ b/src/save.ts @@ -24,9 +24,13 @@ async function saveCache() { const useFallback = getInputAsBoolean("use-fallback"); const paths = getInputAsArray("path"); const local = core.getInput("local"); + const shouldNotStoreInLocal = !local || (local && isLocalHit()) + const shouldNotStoreInRemote = isExactKeyMatch() - if (((local && isLocalHit()) || !local) && isExactKeyMatch()) { + + if (shouldNotStoreInLocal && shouldNotStoreInRemote) { core.info("Cache was exact key match, not saving"); + return; } @@ -50,17 +54,20 @@ async function saveCache() { core.debug(`Archive Path: ${archivePath}`); await createTar(archiveFolder, cachePaths, compressionMethod); + if (core.isDebug()) { await listTar(archivePath, compressionMethod); } - const object = path.join(key, cacheFileName); + if (!shouldNotStoreInRemote) { + const object = path.join(key, cacheFileName); - core.info(`Uploading tar to s3. Bucket: ${bucket}, Object: ${object}`); - await mc.fPutObject(bucket, object, archivePath, {}); - core.info("Cache saved to s3 successfully"); + core.info(`Uploading tar to s3. Bucket: ${bucket}, Object: ${object}`); + await mc.fPutObject(bucket, object, archivePath, {}); + core.info("Cache saved to s3 successfully"); + } - if (local) { + if (!shouldNotStoreInLocal) { core.info("Local cache is enabled"); const localKey = path.join(local, key, cacheFileName);