Skip to content

Commit

Permalink
Add cache-save: false option
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Nov 10, 2023
1 parent aae1567 commit 59d62e4
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
16 changes: 14 additions & 2 deletions __tests__/cache-save.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ describe('run', () => {

beforeEach(() => {
process.env['RUNNER_OS'] = process.env['RUNNER_OS'] ?? 'linux';
for(const key in process.env) {
if(key.startsWith('INPUT_')) {
for (const key in process.env) {
if (key.startsWith('INPUT_')) {
delete process.env[key];
}
}
Expand Down Expand Up @@ -251,6 +251,18 @@ describe('run', () => {
expect(saveCacheSpy).toHaveBeenCalled();
expect(setFailedSpy).not.toHaveBeenCalled();
});

it('should not save the cache when requested not to', async () => {
setInput('cache', 'pip');
setInput('cache-save', 'false');
setInput('python-version', '3.10.0');
await run();
expect(infoSpy).toHaveBeenCalledWith(
'Not saving cache since `cache-save` is false'
);
expect(saveCacheSpy).not.toHaveBeenCalled();
expect(setFailedSpy).not.toHaveBeenCalled();
});
});

afterEach(() => {
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ inputs:
default: ${{ github.server_url == 'https://github.com' && github.token || '' }}
cache-dependency-path:
description: "Used to specify the path to dependency files. Supports wildcards or a list of file names for caching multiple dependencies."
cache-save:
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."
default: true
update-environment:
description: "Set this option if you want the action to update environment variables."
default: true
Expand Down
16 changes: 15 additions & 1 deletion dist/cache-save/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59246,7 +59246,21 @@ function run() {
try {
const cache = core.getInput('cache');
if (cache) {
yield saveCache(cache);
let shouldSave = true;
try {
shouldSave = core.getBooleanInput('cache-save', { required: false });
}
catch (e) {
// If we fail to parse the input, assume it's
// > "Input does not meet YAML 1.2 "core schema" specification."
// and assume it's the `true` default.
}
if (shouldSave) {
yield saveCache(cache);
}
else {
core.info('Not saving cache since `cache-save` is false');
}
}
}
catch (error) {
Expand Down
36 changes: 36 additions & 0 deletions docs/advanced-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,42 @@ steps:
# Or pip install -e '.[test]' to install test dependencies
```

### Skipping cache saving

For some scenarios, it may be useful to only save a given subset of dependencies,
but restore more of them for other workflows. For instance, there may be a heavy
`extras` dependency that you do not need your entire test matrix to download, but
you want to download and test it separately without it being saved in the cache
archive for all runs.

To achieve this, you can use `cache-save: false` on the run that uses the heavy
dependency.


```yaml
test:
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: '3.11'
cache: 'pip'
cache-dependency-path: pyproject.toml
- run: pip install -e .
test-heavy-extra:
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: '3.11'
cache: 'pip'
cache-dependency-path: pyproject.toml
cache-save: false
- run: pip install -e '.[heavy-extra]'
```


# Outputs and environment variables

## Outputs
Expand Down
14 changes: 13 additions & 1 deletion src/cache-save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@ export async function run() {
try {
const cache = core.getInput('cache');
if (cache) {
await saveCache(cache);
let shouldSave = true;
try {
shouldSave = core.getBooleanInput('cache-save', {required: false});
} catch (e) {
// If we fail to parse the input, assume it's
// > "Input does not meet YAML 1.2 "core schema" specification."
// and assume it's the `true` default.
}
if (shouldSave) {
await saveCache(cache);
} else {
core.info('Not saving cache since `cache-save` is false');
}
}
} catch (error) {
const err = error as Error;
Expand Down

0 comments on commit 59d62e4

Please sign in to comment.