Skip to content

Commit 59d62e4

Browse files
committed
Add cache-save: false option
1 parent aae1567 commit 59d62e4

5 files changed

Lines changed: 81 additions & 4 deletions

File tree

__tests__/cache-save.test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ describe('run', () => {
3434

3535
beforeEach(() => {
3636
process.env['RUNNER_OS'] = process.env['RUNNER_OS'] ?? 'linux';
37-
for(const key in process.env) {
38-
if(key.startsWith('INPUT_')) {
37+
for (const key in process.env) {
38+
if (key.startsWith('INPUT_')) {
3939
delete process.env[key];
4040
}
4141
}
@@ -251,6 +251,18 @@ describe('run', () => {
251251
expect(saveCacheSpy).toHaveBeenCalled();
252252
expect(setFailedSpy).not.toHaveBeenCalled();
253253
});
254+
255+
it('should not save the cache when requested not to', async () => {
256+
setInput('cache', 'pip');
257+
setInput('cache-save', 'false');
258+
setInput('python-version', '3.10.0');
259+
await run();
260+
expect(infoSpy).toHaveBeenCalledWith(
261+
'Not saving cache since `cache-save` is false'
262+
);
263+
expect(saveCacheSpy).not.toHaveBeenCalled();
264+
expect(setFailedSpy).not.toHaveBeenCalled();
265+
});
254266
});
255267

256268
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: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59246,7 +59246,21 @@ function run() {
5924659246
try {
5924759247
const cache = core.getInput('cache');
5924859248
if (cache) {
59249-
yield saveCache(cache);
59249+
let shouldSave = true;
59250+
try {
59251+
shouldSave = core.getBooleanInput('cache-save', { required: false });
59252+
}
59253+
catch (e) {
59254+
// If we fail to parse the input, assume it's
59255+
// > "Input does not meet YAML 1.2 "core schema" specification."
59256+
// and assume it's the `true` default.
59257+
}
59258+
if (shouldSave) {
59259+
yield saveCache(cache);
59260+
}
59261+
else {
59262+
core.info('Not saving cache since `cache-save` is false');
59263+
}
5925059264
}
5925159265
}
5925259266
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: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,19 @@ export async function run() {
88
try {
99
const cache = core.getInput('cache');
1010
if (cache) {
11-
await saveCache(cache);
11+
let shouldSave = true;
12+
try {
13+
shouldSave = core.getBooleanInput('cache-save', {required: false});
14+
} catch (e) {
15+
// If we fail to parse the input, assume it's
16+
// > "Input does not meet YAML 1.2 "core schema" specification."
17+
// and assume it's the `true` default.
18+
}
19+
if (shouldSave) {
20+
await saveCache(cache);
21+
} else {
22+
core.info('Not saving cache since `cache-save` is false');
23+
}
1224
}
1325
} catch (error) {
1426
const err = error as Error;

0 commit comments

Comments
 (0)