diff --git a/README.md b/README.md index 3c4e0f5..ca01fa6 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,10 @@ Assuming the underlying executables are available, the allowed values are: * `tgz`: `tar` with gzip compression * `zip`: `(un)zip` compression +### `force` (boolean, optional, save only) + +Force saving the cache even if it exists. Default: `false`. + ### `manifest` (string, required if using `file` caching level) A path to a file or folder that will be hashed to create file-level caches. @@ -131,7 +135,9 @@ steps: manifest: package-lock.json path: node_modules restore: pipeline - save: file + save: + - file + - branch - wait: ~ - label: ':test_tube: Run tests' command: npm test # does not save cache, not necessary diff --git a/hooks/post-command b/hooks/post-command index 320b598..11a23cd 100755 --- a/hooks/post-command +++ b/hooks/post-command @@ -49,6 +49,11 @@ fi for LEVEL in "${SAVE_LEVELS[@]}"; do KEY=$(build_key "${LEVEL}" "${CACHE_PATH}" "${COMPRESS}") - echo "Saving ${LEVEL}-level cache of ${CACHE_PATH}" - backend_exec save "${KEY}" "${ACTUAL_PATH}" + if [ "$(plugin_read_config FORCE 'false')" != 'false' ] || + ! backend_exec exists "${KEY}"; then + echo "Saving ${LEVEL}-level cache of ${CACHE_PATH}" + backend_exec save "${KEY}" "${ACTUAL_PATH}" + else + echo "Cache of ${LEVEL} already exists, skipping" + fi done diff --git a/plugin.yml b/plugin.yml index fa886c8..500b494 100644 --- a/plugin.yml +++ b/plugin.yml @@ -21,6 +21,8 @@ configuration: - zip - tar - tgz + force: + type: boolean manifest: type: string path: diff --git a/tests/post-command-tgz.bats b/tests/post-command-tgz.bats index ee75654..102bc65 100644 --- a/tests/post-command-tgz.bats +++ b/tests/post-command-tgz.bats @@ -15,6 +15,9 @@ setup() { export BUILDKITE_PLUGIN_CACHE_COMPRESSION=tgz export BUILDKITE_PLUGIN_CACHE_PATH=tests/data/my_files + # to make all test easier + export BUILDKITE_PLUGIN_CACHE_FORCE=true + # necessary for key-calculations export BUILDKITE_LABEL="step-label" export BUILDKITE_BRANCH="tests" diff --git a/tests/post-command-zip.bats b/tests/post-command-zip.bats index 14eddc6..7964ea3 100644 --- a/tests/post-command-zip.bats +++ b/tests/post-command-zip.bats @@ -15,6 +15,9 @@ setup() { export BUILDKITE_PLUGIN_CACHE_COMPRESSION=zip export BUILDKITE_PLUGIN_CACHE_PATH=tests/data/my_files + # to make all test easier + export BUILDKITE_PLUGIN_CACHE_FORCE=true + # necessary for key-calculations export BUILDKITE_LABEL="step-label" export BUILDKITE_BRANCH="tests" diff --git a/tests/post-command.bats b/tests/post-command.bats index ed45264..41e1b11 100644 --- a/tests/post-command.bats +++ b/tests/post-command.bats @@ -13,6 +13,9 @@ setup() { export BUILDKITE_PLUGIN_CACHE_BACKEND=dummy export BUILDKITE_PLUGIN_CACHE_PATH=tests/data/my_files + # to make all test easier + export BUILDKITE_PLUGIN_CACHE_FORCE=true + # necessary for key-calculations export BUILDKITE_LABEL="step-label" export BUILDKITE_BRANCH="tests" @@ -179,3 +182,18 @@ teardown() { assert_output --partial 'Invalid cache level unreal' refute_output --partial 'Saving pipeline-level cache' } + +@test "Saving is skipped when cache exists" { + export BUILDKITE_PLUGIN_CACHE_SAVE=all + export BUILDKITE_PLUGIN_CACHE_FORCE='false' + + stub cache_dummy \ + "exists \* \* : exit 0" + + run "$PWD/hooks/post-command" + + assert_success + refute_output --partial 'Saving all-level cache' + + unstub cache_dummy +}