Skip to content

Commit 346329c

Browse files
committed
Add cache-save: false option
1 parent 448593e commit 346329c

5 files changed

Lines changed: 63 additions & 2 deletions

File tree

__tests__/cache-save.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,19 @@ describe('run', () => {
256256
expect(saveCacheSpy).toHaveBeenCalled();
257257
expect(setFailedSpy).not.toHaveBeenCalled();
258258
});
259+
260+
it('should not save the cache when requested not to', async () => {
261+
inputs['cache'] = 'pip';
262+
inputs['cache-save'] = false;
263+
inputs['python-version'] = '3.10.0';
264+
await run();
265+
expect(getInputSpy).toHaveBeenCalled();
266+
expect(infoSpy).toHaveBeenCalledWith(
267+
'Not saving cache since `cache-save` is false'
268+
);
269+
expect(saveCacheSpy).not.toHaveBeenCalled();
270+
expect(setFailedSpy).not.toHaveBeenCalled();
271+
});
259272
});
260273

261274
afterEach(() => {

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ inputs:
2020
default: ${{ github.server_url == 'https://github.com' && github.token || '' }}
2121
cache-dependency-path:
2222
description: "Used to specify the path to dependency files. Supports wildcards or a list of file names for caching multiple dependencies."
23+
cache-save:
24+
description: "Set this option if you want the action to save the cache after the run. Defaults to true. It can be useful to set this to false if you have e.g. optional dependencies that only some workflows require, and they should not be cached."
25+
default: true
2326
update-environment:
2427
description: "Set this option if you want the action to update environment variables."
2528
default: true

dist/cache-save/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59246,7 +59246,12 @@ function run() {
5924659246
try {
5924759247
const cache = core.getInput('cache');
5924859248
if (cache) {
59249-
yield saveCache(cache);
59249+
if (core.getBooleanInput('cache-save')) {
59250+
yield saveCache(cache);
59251+
}
59252+
else {
59253+
core.info('Not saving cache since `cache-save` is false');
59254+
}
5925059255
}
5925159256
}
5925259257
catch (error) {

docs/advanced-usage.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,42 @@ steps:
380380
# Or pip install -e '.[test]' to install test dependencies
381381
```
382382

383+
### Skipping cache saving
384+
385+
For some scenarios, it may be useful to only save a given subset of dependencies,
386+
but restore more of them for other workflows. For instance, there may be a heavy
387+
`extras` dependency that you do not need your entire test matrix to download, but
388+
you want to download and test it separately without it being saved in the cache
389+
archive for all runs.
390+
391+
To achieve this, you can use `cache-save: false` on the run that uses the heavy
392+
dependency.
393+
394+
395+
```yaml
396+
test:
397+
steps:
398+
- uses: actions/checkout@v4
399+
- uses: actions/setup-python@v4
400+
with:
401+
python-version: '3.11'
402+
cache: 'pip'
403+
cache-dependency-path: pyproject.toml
404+
- run: pip install -e .
405+
406+
test-heavy-extra:
407+
steps:
408+
- uses: actions/checkout@v4
409+
- uses: actions/setup-python@v4
410+
with:
411+
python-version: '3.11'
412+
cache: 'pip'
413+
cache-dependency-path: pyproject.toml
414+
cache-save: false
415+
- run: pip install -e '.[heavy-extra]'
416+
```
417+
418+
383419
# Outputs and environment variables
384420

385421
## Outputs

src/cache-save.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ export async function run() {
88
try {
99
const cache = core.getInput('cache');
1010
if (cache) {
11-
await saveCache(cache);
11+
if (core.getBooleanInput('cache-save')) {
12+
await saveCache(cache);
13+
} else {
14+
core.info('Not saving cache since `cache-save` is false');
15+
}
1216
}
1317
} catch (error) {
1418
const err = error as Error;

0 commit comments

Comments
 (0)