Visitar URL original
Disable automatic cache saves for merge queues (#1056) · astral-sh/setup-uv@a761a4e · GitHub
Skip to content

Commit a761a4e

Browse files
eifingerampagent
andauthored
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>
1 parent 3377a30 commit a761a4e

8 files changed

Lines changed: 95 additions & 10 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ Have a look under [Advanced Configuration](#advanced-configuration) for detailed
9090
# Whether to restore the cache if found
9191
restore-cache: "true"
9292
93-
# Whether to save the cache after the run
94-
save-cache: "true"
93+
# Whether to save the cache after the run: true, false, or auto (disabled for merge_group events)
94+
save-cache: "auto"
9595
9696
# Suffix for the cache key
9797
cache-suffix: ""

‎__tests__/utils/inputs.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,51 @@ describe("loadInputs", () => {
206206
expect(inputs.enableCache).toBe(true);
207207
});
208208

209+
it("restores but does not save cache automatically for merge groups", () => {
210+
mockInputs["working-directory"] = "/workspace";
211+
mockInputs["enable-cache"] = "auto";
212+
mockInputs["restore-cache"] = "true";
213+
mockInputs["save-cache"] = "auto";
214+
process.env.RUNNER_ENVIRONMENT = "github-hosted";
215+
process.env.RUNNER_TEMP = "/runner-temp";
216+
process.env.GITHUB_EVENT_NAME = "merge_group";
217+
218+
const inputs = loadInputs();
219+
220+
expect(inputs.enableCache).toBe(true);
221+
expect(inputs.restoreCache).toBe(true);
222+
expect(inputs.saveCache).toBe(false);
223+
expect(mockInfo).toHaveBeenCalledWith(
224+
"Cache saving is disabled for the merge_group event",
225+
);
226+
});
227+
228+
it.each([
229+
["true", true],
230+
["false", false],
231+
])("honors save-cache %s for merge groups", (saveCacheInput, expected) => {
232+
mockInputs["working-directory"] = "/workspace";
233+
mockInputs["save-cache"] = saveCacheInput;
234+
process.env.GITHUB_EVENT_NAME = "merge_group";
235+
236+
const inputs = loadInputs();
237+
238+
expect(inputs.saveCache).toBe(expected);
239+
expect(mockInfo).not.toHaveBeenCalledWith(
240+
"Cache saving is disabled for the merge_group event",
241+
);
242+
});
243+
244+
it("automatically saves cache for other events", () => {
245+
mockInputs["working-directory"] = "/workspace";
246+
mockInputs["save-cache"] = "auto";
247+
process.env.GITHUB_EVENT_NAME = "push";
248+
249+
const inputs = loadInputs();
250+
251+
expect(inputs.saveCache).toBe(true);
252+
});
253+
209254
it("uses cache-dir from pyproject.toml when present", () => {
210255
mockInputs["working-directory"] = createTempProject({
211256
"pyproject.toml": `[project]

‎action-types.yml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ inputs:
3333
restore-cache:
3434
type: boolean
3535
save-cache:
36-
type: boolean
36+
type: enum
37+
allowed-values:
38+
- "true"
39+
- "false"
40+
- auto
3741
cache-suffix:
3842
type: string
3943
cache-local-path:

‎action.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ inputs:
5151
description: "Whether to restore the cache if found."
5252
default: "true"
5353
save-cache:
54-
description: "Whether to save the cache after the run."
55-
default: "true"
54+
description: "Whether to save the cache after the run. 'auto' disables saving for merge_group events."
55+
default: "auto"
5656
cache-suffix:
5757
description: "Suffix for the cache key"
5858
required: false

‎dist/save-cache/index.cjs‎

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/setup/index.cjs‎

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/caching.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,11 @@ By default, the cache will be restored.
139139

140140
## Save cache
141141

142-
You can also disable saving the cache after the run with the `save-cache` input.
142+
You can control saving the cache after the run with the `save-cache` input.
143143
This can be useful to save cache storage when you know you will not use the cache of the run again.
144-
By default, the cache will be saved.
144+
By default, `save-cache: auto` saves the cache except for `merge_group` events, where caches created
145+
for temporary merge queue refs are unlikely to be reused. Cache restoration remains enabled for
146+
these events. Set `save-cache: true` to save the cache for all events.
145147

146148
```yaml
147149
- name: Don't save the cache after the run

‎src/utils/inputs.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function loadInputs(): SetupInputs {
6060
const checksum = core.getInput("checksum");
6161
const enableCache = getEnableCache();
6262
const restoreCache = core.getInput("restore-cache") === "true";
63-
const saveCache = core.getInput("save-cache") === "true";
63+
const saveCache = getSaveCache();
6464
const cacheSuffix = core.getInput("cache-suffix") || "";
6565
const cacheLocalPath = getCacheLocalPath(
6666
workingDirectory,
@@ -189,6 +189,18 @@ function getEnableCache(): boolean {
189189
return enableCacheInput === "true";
190190
}
191191

192+
function getSaveCache(): boolean {
193+
const saveCacheInput = core.getInput("save-cache");
194+
if (saveCacheInput === "auto") {
195+
if (process.env.GITHUB_EVENT_NAME === "merge_group") {
196+
log.info("Cache saving is disabled for the merge_group event");
197+
return false;
198+
}
199+
return true;
200+
}
201+
return saveCacheInput === "true";
202+
}
203+
192204
function getToolBinDir(workingDirectory: string): string | undefined {
193205
const toolBinDirInput = core.getInput("tool-bin-dir");
194206
if (toolBinDirInput !== "") {

0 commit comments

Comments
 (0)