Add `cache-save: false` option

This commit is contained in:
Aarni Koskela 2023-11-10 10:24:46 +02:00
parent 7e8faf07b9
commit 8f0a0b0eda
5 changed files with 85 additions and 9 deletions

View File

@ -34,8 +34,8 @@ describe('run', () => {
beforeEach(() => { beforeEach(() => {
process.env['RUNNER_OS'] = process.env['RUNNER_OS'] ?? 'linux'; process.env['RUNNER_OS'] = process.env['RUNNER_OS'] ?? 'linux';
for(const key in process.env) { for (const key in process.env) {
if(key.startsWith('INPUT_')) { if (key.startsWith('INPUT_')) {
delete process.env[key]; delete process.env[key];
} }
} }
@ -251,6 +251,18 @@ describe('run', () => {
expect(saveCacheSpy).toHaveBeenCalled(); expect(saveCacheSpy).toHaveBeenCalled();
expect(setFailedSpy).not.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(() => { afterEach(() => {

View File

@ -20,6 +20,9 @@ inputs:
default: ${{ github.server_url == 'https://github.com' && github.token || '' }} default: ${{ github.server_url == 'https://github.com' && github.token || '' }}
cache-dependency-path: cache-dependency-path:
description: "Used to specify the path to dependency files. Supports wildcards or a list of file names for caching multiple dependencies." 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: update-environment:
description: "Set this option if you want the action to update environment variables." description: "Set this option if you want the action to update environment variables."
default: true default: true

View File

@ -80475,9 +80475,23 @@ function run(earlyExit) {
try { try {
const cache = core.getInput('cache'); const cache = core.getInput('cache');
if (cache) { if (cache) {
yield saveCache(cache); let shouldSave = true;
if (earlyExit) { try {
process.exit(0); 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);
if (earlyExit) {
process.exit(0);
}
}
else {
core.info('Not saving cache since `cache-save` is false');
} }
} }
} }

View File

@ -381,6 +381,42 @@ steps:
# Or pip install -e '.[test]' to install test dependencies # 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 and environment variables
## Outputs ## Outputs

View File

@ -11,10 +11,21 @@ export async function run(earlyExit?: boolean) {
try { try {
const cache = core.getInput('cache'); const cache = core.getInput('cache');
if (cache) { if (cache) {
await saveCache(cache); let shouldSave = true;
try {
if (earlyExit) { shouldSave = core.getBooleanInput('cache-save', {required: false});
process.exit(0); } 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);
if (earlyExit) {
process.exit(0);
}
} else {
core.info('Not saving cache since `cache-save` is false');
} }
} }
} catch (error) { } catch (error) {