Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: ""
Expand Down
45 changes: 45 additions & 0 deletions __tests__/utils/inputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 5 additions & 1 deletion action-types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion dist/save-cache/index.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion dist/setup/index.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions docs/caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion src/utils/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 !== "") {
Expand Down
Loading