From 346329c4c18ada5b5a884879b45ee6894ba7a346 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Fri, 10 Nov 2023 10:24:46 +0200 Subject: [PATCH] Add `cache-save: false` option --- __tests__/cache-save.test.ts | 13 +++++++++++++ action.yml | 3 +++ dist/cache-save/index.js | 7 ++++++- docs/advanced-usage.md | 36 ++++++++++++++++++++++++++++++++++++ src/cache-save.ts | 6 +++++- 5 files changed, 63 insertions(+), 2 deletions(-) diff --git a/__tests__/cache-save.test.ts b/__tests__/cache-save.test.ts index 26f7da24b..9765e0733 100644 --- a/__tests__/cache-save.test.ts +++ b/__tests__/cache-save.test.ts @@ -256,6 +256,19 @@ describe('run', () => { expect(saveCacheSpy).toHaveBeenCalled(); expect(setFailedSpy).not.toHaveBeenCalled(); }); + + it('should not save the cache when requested not to', async () => { + inputs['cache'] = 'pip'; + inputs['cache-save'] = false; + inputs['python-version'] = '3.10.0'; + await run(); + expect(getInputSpy).toHaveBeenCalled(); + expect(infoSpy).toHaveBeenCalledWith( + 'Not saving cache since `cache-save` is false' + ); + expect(saveCacheSpy).not.toHaveBeenCalled(); + expect(setFailedSpy).not.toHaveBeenCalled(); + }); }); afterEach(() => { diff --git a/action.yml b/action.yml index 3a6531c88..e19eb2ceb 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/dist/cache-save/index.js b/dist/cache-save/index.js index b5f791503..d639afade 100644 --- a/dist/cache-save/index.js +++ b/dist/cache-save/index.js @@ -59246,7 +59246,12 @@ function run() { try { const cache = core.getInput('cache'); if (cache) { - yield saveCache(cache); + if (core.getBooleanInput('cache-save')) { + yield saveCache(cache); + } + else { + core.info('Not saving cache since `cache-save` is false'); + } } } catch (error) { diff --git a/docs/advanced-usage.md b/docs/advanced-usage.md index 0918a33d7..edcd76913 100644 --- a/docs/advanced-usage.md +++ b/docs/advanced-usage.md @@ -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 diff --git a/src/cache-save.ts b/src/cache-save.ts index 1017ef11d..4c42afb28 100644 --- a/src/cache-save.ts +++ b/src/cache-save.ts @@ -8,7 +8,11 @@ export async function run() { try { const cache = core.getInput('cache'); if (cache) { - await saveCache(cache); + if (core.getBooleanInput('cache-save')) { + await saveCache(cache); + } else { + core.info('Not saving cache since `cache-save` is false'); + } } } catch (error) { const err = error as Error;