From 91d3f2deb08f6a7c01614800305c3d89b8357705 Mon Sep 17 00:00:00 2001
From: Sankalp Kotewar <98868223+kotewar@users.noreply.github.com>
Date: Mon, 21 Nov 2022 08:45:08 +0000
Subject: [PATCH] Saving state early to take care of all cases

---
 dist/restore/index.js | 11 ++++++-----
 src/restore.ts        | 18 +++++++++---------
 2 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/dist/restore/index.js b/dist/restore/index.js
index bb55c86..5426bc7 100644
--- a/dist/restore/index.js
+++ b/dist/restore/index.js
@@ -48987,6 +48987,12 @@ function run() {
                 required: true
             });
             const cacheKey = yield cache.restoreCache(cachePaths, primaryKey, restoreKeys);
+            //Check if user wants to save cache despite of failure in any previous job
+            const saveCache = core.getInput(constants_1.Inputs.SaveCacheOnAnyFailure);
+            if (saveCache === "yes") {
+                core.saveState(constants_1.State.SaveCache, saveCache);
+                core.info(`Input save-cache-on-any-failure is set to yes, the cache will be saved despite of any failure in the build.`);
+            }
             if (!cacheKey) {
                 if (core.getInput(constants_1.Inputs.StrictRestore) == "true") {
                     throw new Error(`Cache with the given input key ${primaryKey} is not found, hence exiting the workflow as the strict-restore requirement is not met.`);
@@ -49005,11 +49011,6 @@ function run() {
                 throw new Error(`Restored cache key doesn't match the given input key ${primaryKey}, hence exiting the workflow as the strict-restore requirement is not met.`);
             }
             core.info(`Cache restored from key: ${cacheKey}`);
-            const saveCache = core.getInput(constants_1.Inputs.SaveCacheOnAnyFailure);
-            if (saveCache === "yes") {
-                core.saveState(constants_1.State.SaveCache, saveCache);
-                core.info(`Input save-cache-on-any-failure is set to yes, the cache will be saved despite of any failure in the build.`);
-            }
         }
         catch (error) {
             core.setFailed(error.message);
diff --git a/src/restore.ts b/src/restore.ts
index 0ae4ca6..4814a83 100644
--- a/src/restore.ts
+++ b/src/restore.ts
@@ -35,6 +35,15 @@ async function run(): Promise<void> {
             restoreKeys
         );
 
+        //Check if user wants to save cache despite of failure in any previous job
+        const saveCache = core.getInput(Inputs.SaveCacheOnAnyFailure);
+        if (saveCache === "yes") {
+            core.saveState(State.SaveCache, saveCache);
+            core.info(
+                `Input save-cache-on-any-failure is set to yes, the cache will be saved despite of any failure in the build.`
+            );
+        }
+
         if (!cacheKey) {
             if (core.getInput(Inputs.StrictRestore) == "true") {
                 throw new Error(
@@ -63,15 +72,6 @@ async function run(): Promise<void> {
             );
         }
         core.info(`Cache restored from key: ${cacheKey}`);
-
-        const saveCache = core.getInput(Inputs.SaveCacheOnAnyFailure);
-
-        if (saveCache === "yes") {
-            core.saveState(State.SaveCache, saveCache);
-            core.info(
-                `Input save-cache-on-any-failure is set to yes, the cache will be saved despite of any failure in the build.`
-            );
-        }
     } catch (error: unknown) {
         core.setFailed((error as Error).message);
     }