Disable automatic cache saves for merge queues (#1056)

Amp-Thread-ID:
https://ampcode.com/threads/T-01a0af68-f950-77dc-b9ae-9402987584c8

Closes: #1052

Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Kevin Stillhammer
2026-09-17 15:56:38 +02:00
committed by GitHub
parent 3377a30666
commit a761a4e9af
8 changed files with 95 additions and 10 deletions
+2 -2
View File
@@ -90,8 +90,8 @@ Have a look under [Advanced Configuration](#advanced-configuration) for detailed
# Whether to restore the cache if found # Whether to restore the cache if found
restore-cache: "true" restore-cache: "true"
# Whether to save the cache after the run # Whether to save the cache after the run: true, false, or auto (disabled for merge_group events)
save-cache: "true" save-cache: "auto"
# Suffix for the cache key # Suffix for the cache key
cache-suffix: "" cache-suffix: ""
+45
View File
@@ -206,6 +206,51 @@ describe("loadInputs", () => {
expect(inputs.enableCache).toBe(true); 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", () => { it("uses cache-dir from pyproject.toml when present", () => {
mockInputs["working-directory"] = createTempProject({ mockInputs["working-directory"] = createTempProject({
"pyproject.toml": `[project] "pyproject.toml": `[project]
+5 -1
View File
@@ -33,7 +33,11 @@ inputs:
restore-cache: restore-cache:
type: boolean type: boolean
save-cache: save-cache:
type: boolean type: enum
allowed-values:
- "true"
- "false"
- auto
cache-suffix: cache-suffix:
type: string type: string
cache-local-path: cache-local-path:
+2 -2
View File
@@ -51,8 +51,8 @@ inputs:
description: "Whether to restore the cache if found." description: "Whether to restore the cache if found."
default: "true" default: "true"
save-cache: save-cache:
description: "Whether to save the cache after the run." description: "Whether to save the cache after the run. 'auto' disables saving for merge_group events."
default: "true" default: "auto"
cache-suffix: cache-suffix:
description: "Suffix for the cache key" description: "Suffix for the cache key"
required: false required: false
Generated Vendored
+12 -1
View File
@@ -64490,7 +64490,7 @@ function loadInputs() {
const checksum = getInput("checksum"); const checksum = getInput("checksum");
const enableCache = getEnableCache(); const enableCache = getEnableCache();
const restoreCache2 = getInput("restore-cache") === "true"; const restoreCache2 = getInput("restore-cache") === "true";
const saveCache4 = getInput("save-cache") === "true"; const saveCache4 = getSaveCache();
const cacheSuffix = getInput("cache-suffix") || ""; const cacheSuffix = getInput("cache-suffix") || "";
const cacheLocalPath = getCacheLocalPath( const cacheLocalPath = getCacheLocalPath(
workingDirectory, workingDirectory,
@@ -64600,6 +64600,17 @@ function getEnableCache() {
} }
return enableCacheInput === "true"; 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) { function getToolBinDir(workingDirectory) {
const toolBinDirInput = getInput("tool-bin-dir"); const toolBinDirInput = getInput("tool-bin-dir");
if (toolBinDirInput !== "") { if (toolBinDirInput !== "") {
Generated Vendored
+12 -1
View File
@@ -101889,7 +101889,7 @@ function loadInputs() {
const checksum = getInput("checksum"); const checksum = getInput("checksum");
const enableCache = getEnableCache(); const enableCache = getEnableCache();
const restoreCache3 = getInput("restore-cache") === "true"; const restoreCache3 = getInput("restore-cache") === "true";
const saveCache2 = getInput("save-cache") === "true"; const saveCache2 = getSaveCache();
const cacheSuffix = getInput("cache-suffix") || ""; const cacheSuffix = getInput("cache-suffix") || "";
const cacheLocalPath = getCacheLocalPath( const cacheLocalPath = getCacheLocalPath(
workingDirectory, workingDirectory,
@@ -101999,6 +101999,17 @@ function getEnableCache() {
} }
return enableCacheInput === "true"; 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) { function getToolBinDir(workingDirectory) {
const toolBinDirInput = getInput("tool-bin-dir"); const toolBinDirInput = getInput("tool-bin-dir");
if (toolBinDirInput !== "") { if (toolBinDirInput !== "") {
+4 -2
View File
@@ -139,9 +139,11 @@ By default, the cache will be restored.
## Save cache ## 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. 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 ```yaml
- name: Don't save the cache after the run - name: Don't save the cache after the run
+13 -1
View File
@@ -60,7 +60,7 @@ export function loadInputs(): SetupInputs {
const checksum = core.getInput("checksum"); const checksum = core.getInput("checksum");
const enableCache = getEnableCache(); const enableCache = getEnableCache();
const restoreCache = core.getInput("restore-cache") === "true"; const restoreCache = core.getInput("restore-cache") === "true";
const saveCache = core.getInput("save-cache") === "true"; const saveCache = getSaveCache();
const cacheSuffix = core.getInput("cache-suffix") || ""; const cacheSuffix = core.getInput("cache-suffix") || "";
const cacheLocalPath = getCacheLocalPath( const cacheLocalPath = getCacheLocalPath(
workingDirectory, workingDirectory,
@@ -189,6 +189,18 @@ function getEnableCache(): boolean {
return enableCacheInput === "true"; 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 { function getToolBinDir(workingDirectory: string): string | undefined {
const toolBinDirInput = core.getInput("tool-bin-dir"); const toolBinDirInput = core.getInput("tool-bin-dir");
if (toolBinDirInput !== "") { if (toolBinDirInput !== "") {