Skip to content

Commit dc724a1

Browse files
authored
Add inputs restore-cache and save-cache (#568)
Closes: #555
1 parent f67343a commit dc724a1

File tree

8 files changed

+149
-9
lines changed

8 files changed

+149
-9
lines changed

.github/workflows/test.yml

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ jobs:
457457
working-directory: __tests__/fixtures/requirements-txt-project
458458
test-restore-cache-requirements-txt:
459459
runs-on: ubuntu-latest
460-
needs: test-setup-cache
460+
needs: test-setup-cache-requirements-txt
461461
steps:
462462
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
463463
with:
@@ -525,6 +525,78 @@ jobs:
525525
env:
526526
CACHE_HIT: ${{ steps.restore.outputs.cache-hit }}
527527

528+
test-setup-cache-save-cache-false:
529+
runs-on: ubuntu-latest
530+
steps:
531+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
532+
with:
533+
persist-credentials: false
534+
- name: Setup with cache
535+
uses: ./
536+
with:
537+
enable-cache: true
538+
save-cache: false
539+
cache-suffix: ${{ github.run_id }}-${{ github.run_attempt }}-test-setup-cache-save-cache-false
540+
- run: uv sync
541+
working-directory: __tests__/fixtures/uv-project
542+
shell: bash
543+
test-restore-cache-save-cache-false:
544+
runs-on: ubuntu-latest
545+
needs: test-setup-cache-save-cache-false
546+
steps:
547+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
548+
with:
549+
persist-credentials: false
550+
- name: Restore with cache
551+
id: restore
552+
uses: ./
553+
with:
554+
enable-cache: true
555+
cache-suffix: ${{ github.run_id }}-${{ github.run_attempt }}-test-setup-cache-save-cache-false
556+
- name: Cache was not hit
557+
run: |
558+
if [ "$CACHE_HIT" == "true" ]; then
559+
exit 1
560+
fi
561+
env:
562+
CACHE_HIT: ${{ steps.restore.outputs.cache-hit }}
563+
564+
test-setup-cache-restore-cache-false:
565+
runs-on: ubuntu-latest
566+
steps:
567+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
568+
with:
569+
persist-credentials: false
570+
- name: Setup with cache
571+
uses: ./
572+
with:
573+
enable-cache: true
574+
cache-suffix: ${{ github.run_id }}-${{ github.run_attempt }}-test-setup-cache-restore-cache-false
575+
- run: uv sync
576+
working-directory: __tests__/fixtures/uv-project
577+
shell: bash
578+
test-restore-cache-restore-cache-false:
579+
runs-on: ubuntu-latest
580+
needs: test-setup-cache-restore-cache-false
581+
steps:
582+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
583+
with:
584+
persist-credentials: false
585+
- name: Restore with cache
586+
id: restore
587+
uses: ./
588+
with:
589+
enable-cache: true
590+
restore-cache: false
591+
cache-suffix: ${{ github.run_id }}-${{ github.run_attempt }}-test-setup-cache-restore-cache-false
592+
- name: Cache was not hit
593+
run: |
594+
if [ "$CACHE_HIT" == "true" ]; then
595+
exit 1
596+
fi
597+
env:
598+
CACHE_HIT: ${{ steps.restore.outputs.cache-hit }}
599+
528600
test-cache-local:
529601
strategy:
530602
matrix:
@@ -747,6 +819,10 @@ jobs:
747819
- test-restore-cache-requirements-txt
748820
- test-setup-cache-dependency-glob
749821
- test-restore-cache-dependency-glob
822+
- test-setup-cache-save-cache-false
823+
- test-restore-cache-save-cache-false
824+
- test-setup-cache-restore-cache-false
825+
- test-restore-cache-restore-cache-false
750826
- test-setup-cache-local
751827
- test-restore-cache-local
752828
- test-tilde-expansion-cache-local-path

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Set up your GitHub Actions workflow with a specific version of [uv](https://docs
2222
- [Validate checksum](#validate-checksum)
2323
- [Enable Caching](#enable-caching)
2424
- [Cache dependency glob](#cache-dependency-glob)
25+
- [Restore cache](#restore-cache)
26+
- [Save cache](#save-cache)
2527
- [Local cache path](#local-cache-path)
2628
- [Disable cache pruning](#disable-cache-pruning)
2729
- [Ignore nothing to cache](#ignore-nothing-to-cache)
@@ -284,6 +286,33 @@ changes. If you use relative paths, they are relative to the repository root.
284286
cache-dependency-glob: ""
285287
```
286288

289+
#### Restore cache
290+
291+
Restoring an existing cache can be enabled or disabled with the `restore-cache` input.
292+
By default, the cache will be restored.
293+
294+
```yaml
295+
- name: Don't restore an existing cache
296+
uses: astral-sh/setup-uv@v6
297+
with:
298+
enable-cache: true
299+
restore-cache: false
300+
```
301+
302+
#### Save cache
303+
304+
You can also disable saving the cache after the run with the `save-cache` input.
305+
This can be useful to save cache storage when you know you will not use the cache of the run again.
306+
By default, the cache will be saved.
307+
308+
```yaml
309+
- name: Don't save the cache after the run
310+
uses: astral-sh/setup-uv@v6
311+
with:
312+
enable-cache: true
313+
save-cache: false
314+
```
315+
287316
### Local cache path
288317

289318
This action controls where uv stores its cache on the runner's filesystem by setting `UV_CACHE_DIR`.

action.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ inputs:
4444
**/*constraints*.in
4545
**/pyproject.toml
4646
**/uv.lock
47+
restore-cache:
48+
description: "Whether to restore the cache if found."
49+
default: "true"
50+
save-cache:
51+
description: "Whether to save the cache after the run."
52+
default: "true"
4753
cache-suffix:
4854
description: "Suffix for the cache key"
4955
required: false

dist/save-cache/index.js

Lines changed: 14 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/setup/index.js

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cache/restore-cache.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
cacheSuffix,
99
pruneCache,
1010
pythonVersion as pythonVersionInput,
11+
restoreCache as shouldRestoreCache,
1112
workingDirectory,
1213
} from "../utils/inputs";
1314
import { getArch, getPlatform } from "../utils/platforms";
@@ -18,6 +19,12 @@ const CACHE_VERSION = "1";
1819

1920
export async function restoreCache(): Promise<void> {
2021
const cacheKey = await computeKeys();
22+
core.saveState(STATE_CACHE_KEY, cacheKey);
23+
24+
if (!shouldRestoreCache) {
25+
core.info("restore-cache is false. Skipping restore cache step.");
26+
return;
27+
}
2128

2229
let matchedKey: string | undefined;
2330
core.info(
@@ -32,8 +39,6 @@ export async function restoreCache(): Promise<void> {
3239
return;
3340
}
3441

35-
core.saveState(STATE_CACHE_KEY, cacheKey);
36-
3742
handleMatchResult(matchedKey, cacheKey);
3843
}
3944

src/save-cache.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@ import {
1111
enableCache,
1212
ignoreNothingToCache,
1313
pruneCache as shouldPruneCache,
14+
saveCache as shouldSaveCache,
1415
} from "./utils/inputs";
1516

1617
export async function run(): Promise<void> {
1718
try {
1819
if (enableCache) {
19-
await saveCache();
20+
if (shouldSaveCache) {
21+
await saveCache();
22+
} else {
23+
core.info("save-cache is false. Skipping save cache step.");
24+
}
2025
// node will stay alive if any promises are not resolved,
2126
// which is a possibility if HTTP requests are dangling
2227
// due to retries or timeouts. We know that if we got here

src/utils/inputs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export const pythonVersion = core.getInput("python-version");
88
export const activateEnvironment = core.getBooleanInput("activate-environment");
99
export const checkSum = core.getInput("checksum");
1010
export const enableCache = getEnableCache();
11+
export const restoreCache = core.getInput("restore-cache") === "true";
12+
export const saveCache = core.getInput("save-cache") === "true";
1113
export const cacheSuffix = core.getInput("cache-suffix") || "";
1214
export const cacheLocalPath = getCacheLocalPath();
1315
export const cacheDependencyGlob = getCacheDependencyGlob();

0 commit comments

Comments
 (0)