diff --git a/README.md b/README.md index 566cb25..7e9e522 100644 --- a/README.md +++ b/README.md @@ -90,8 +90,8 @@ Have a look under [Advanced Configuration](#advanced-configuration) for detailed # Whether to restore the cache if found restore-cache: "true" - # Whether to save the cache after the run - save-cache: "true" + # Whether to save the cache after the run: true, false, or auto (disabled for merge_group events) + save-cache: "auto" # Suffix for the cache key cache-suffix: "" diff --git a/__tests__/utils/inputs.test.ts b/__tests__/utils/inputs.test.ts index aad3495..f93a41b 100644 --- a/__tests__/utils/inputs.test.ts +++ b/__tests__/utils/inputs.test.ts @@ -206,6 +206,51 @@ describe("loadInputs", () => { expect(inputs.enableCache).toBe(true); }); + it("restores but does not save cache automatically for merge groups", () => { + mockInputs["working-directory"] = "/workspace"; + mockInputs["enable-cache"] = "auto"; + mockInputs["restore-cache"] = "true"; + mockInputs["save-cache"] = "auto"; + process.env.RUNNER_ENVIRONMENT = "github-hosted"; + process.env.RUNNER_TEMP = "/runner-temp"; + process.env.GITHUB_EVENT_NAME = "merge_group"; + + const inputs = loadInputs(); + + expect(inputs.enableCache).toBe(true); + expect(inputs.restoreCache).toBe(true); + expect(inputs.saveCache).toBe(false); + expect(mockInfo).toHaveBeenCalledWith( + "Cache saving is disabled for the merge_group event", + ); + }); + + it.each([ + ["true", true], + ["false", false], + ])("honors save-cache %s for merge groups", (saveCacheInput, expected) => { + mockInputs["working-directory"] = "/workspace"; + mockInputs["save-cache"] = saveCacheInput; + process.env.GITHUB_EVENT_NAME = "merge_group"; + + const inputs = loadInputs(); + + expect(inputs.saveCache).toBe(expected); + expect(mockInfo).not.toHaveBeenCalledWith( + "Cache saving is disabled for the merge_group event", + ); + }); + + it("automatically saves cache for other events", () => { + mockInputs["working-directory"] = "/workspace"; + mockInputs["save-cache"] = "auto"; + process.env.GITHUB_EVENT_NAME = "push"; + + const inputs = loadInputs(); + + expect(inputs.saveCache).toBe(true); + }); + it("uses cache-dir from pyproject.toml when present", () => { mockInputs["working-directory"] = createTempProject({ "pyproject.toml": `[project] diff --git a/action-types.yml b/action-types.yml index 0be5368..a2b4ac3 100644 --- a/action-types.yml +++ b/action-types.yml @@ -33,7 +33,11 @@ inputs: restore-cache: type: boolean save-cache: - type: boolean + type: enum + allowed-values: + - "true" + - "false" + - auto cache-suffix: type: string cache-local-path: diff --git a/action.yml b/action.yml index 2a51899..835f8bd 100644 --- a/action.yml +++ b/action.yml @@ -51,8 +51,8 @@ inputs: description: "Whether to restore the cache if found." default: "true" save-cache: - description: "Whether to save the cache after the run." - default: "true" + description: "Whether to save the cache after the run. 'auto' disables saving for merge_group events." + default: "auto" cache-suffix: description: "Suffix for the cache key" required: false diff --git a/dist/save-cache/index.cjs b/dist/save-cache/index.cjs index 1daef0e..7f6c04d 100644 --- a/dist/save-cache/index.cjs +++ b/dist/save-cache/index.cjs @@ -64490,7 +64490,7 @@ function loadInputs() { const checksum = getInput("checksum"); const enableCache = getEnableCache(); const restoreCache2 = getInput("restore-cache") === "true"; - const saveCache4 = getInput("save-cache") === "true"; + const saveCache4 = getSaveCache(); const cacheSuffix = getInput("cache-suffix") || ""; const cacheLocalPath = getCacheLocalPath( workingDirectory, @@ -64600,6 +64600,17 @@ function getEnableCache() { } return enableCacheInput === "true"; } +function getSaveCache() { + const saveCacheInput = getInput("save-cache"); + if (saveCacheInput === "auto") { + if (process.env.GITHUB_EVENT_NAME === "merge_group") { + info2("Cache saving is disabled for the merge_group event"); + return false; + } + return true; + } + return saveCacheInput === "true"; +} function getToolBinDir(workingDirectory) { const toolBinDirInput = getInput("tool-bin-dir"); if (toolBinDirInput !== "") { diff --git a/dist/setup/index.cjs b/dist/setup/index.cjs index d253368..eb12e84 100644 --- a/dist/setup/index.cjs +++ b/dist/setup/index.cjs @@ -101889,7 +101889,7 @@ function loadInputs() { const checksum = getInput("checksum"); const enableCache = getEnableCache(); const restoreCache3 = getInput("restore-cache") === "true"; - const saveCache2 = getInput("save-cache") === "true"; + const saveCache2 = getSaveCache(); const cacheSuffix = getInput("cache-suffix") || ""; const cacheLocalPath = getCacheLocalPath( workingDirectory, @@ -101999,6 +101999,17 @@ function getEnableCache() { } return enableCacheInput === "true"; } +function getSaveCache() { + const saveCacheInput = getInput("save-cache"); + if (saveCacheInput === "auto") { + if (process.env.GITHUB_EVENT_NAME === "merge_group") { + info2("Cache saving is disabled for the merge_group event"); + return false; + } + return true; + } + return saveCacheInput === "true"; +} function getToolBinDir(workingDirectory) { const toolBinDirInput = getInput("tool-bin-dir"); if (toolBinDirInput !== "") { diff --git a/docs/caching.md b/docs/caching.md index 61eca66..c38d4b2 100644 --- a/docs/caching.md +++ b/docs/caching.md @@ -139,9 +139,11 @@ By default, the cache will be restored. ## Save cache -You can also disable saving the cache after the run with the `save-cache` input. +You can control saving the cache after the run with the `save-cache` input. This can be useful to save cache storage when you know you will not use the cache of the run again. -By default, the cache will be saved. +By default, `save-cache: auto` saves the cache except for `merge_group` events, where caches created +for temporary merge queue refs are unlikely to be reused. Cache restoration remains enabled for +these events. Set `save-cache: true` to save the cache for all events. ```yaml - name: Don't save the cache after the run diff --git a/src/utils/inputs.ts b/src/utils/inputs.ts index 74a75d8..62ddfc2 100644 --- a/src/utils/inputs.ts +++ b/src/utils/inputs.ts @@ -60,7 +60,7 @@ export function loadInputs(): SetupInputs { const checksum = core.getInput("checksum"); const enableCache = getEnableCache(); const restoreCache = core.getInput("restore-cache") === "true"; - const saveCache = core.getInput("save-cache") === "true"; + const saveCache = getSaveCache(); const cacheSuffix = core.getInput("cache-suffix") || ""; const cacheLocalPath = getCacheLocalPath( workingDirectory, @@ -189,6 +189,18 @@ function getEnableCache(): boolean { return enableCacheInput === "true"; } +function getSaveCache(): boolean { + const saveCacheInput = core.getInput("save-cache"); + if (saveCacheInput === "auto") { + if (process.env.GITHUB_EVENT_NAME === "merge_group") { + log.info("Cache saving is disabled for the merge_group event"); + return false; + } + return true; + } + return saveCacheInput === "true"; +} + function getToolBinDir(workingDirectory: string): string | undefined { const toolBinDirInput = core.getInput("tool-bin-dir"); if (toolBinDirInput !== "") {