Skip to content

Commit 8f0a0b0

Browse files
committed
Add cache-save: false option
1 parent 7e8faf0 commit 8f0a0b0

5 files changed

Lines changed: 85 additions & 9 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: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80475,9 +80475,23 @@ function run(earlyExit) {
8047580475
try {
8047680476
const cache = core.getInput('cache');
8047780477
if (cache) {
80478-
yield saveCache(cache);
80479-
if (earlyExit) {
80480-
process.exit(0);
80478+
let shouldSave = true;
80479+
try {
80480+
shouldSave = core.getBooleanInput('cache-save', { required: false });
80481+
}
80482+
catch (e) {
80483+
// If we fail to parse the input, assume it's
80484+
// > "Input does not meet YAML 1.2 "core schema" specification."
80485+
// and assume it's the `true` default.
80486+
}
80487+
if (shouldSave) {
80488+
yield saveCache(cache);
80489+
if (earlyExit) {
80490+
process.exit(0);
80491+
}
80492+
}
80493+
else {
80494+
core.info('Not saving cache since `cache-save` is false');
8048180495
}
8048280496
}
8048380497
}

docs/advanced-usage.md

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

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

386422
## Outputs

src/cache-save.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,21 @@ export async function run(earlyExit?: boolean) {
1111
try {
1212
const cache = core.getInput('cache');
1313
if (cache) {
14-
await saveCache(cache);
15-
16-
if (earlyExit) {
17-
process.exit(0);
14+
let shouldSave = true;
15+
try {
16+
shouldSave = core.getBooleanInput('cache-save', {required: false});
17+
} catch (e) {
18+
// If we fail to parse the input, assume it's
19+
// > "Input does not meet YAML 1.2 "core schema" specification."
20+
// and assume it's the `true` default.
21+
}
22+
if (shouldSave) {
23+
await saveCache(cache);
24+
if (earlyExit) {
25+
process.exit(0);
26+
}
27+
} else {
28+
core.info('Not saving cache since `cache-save` is false');
1829
}
1930
}
2031
} catch (error) {

0 commit comments

Comments
 (0)