Skip to content

Commit

Permalink
Merge pull request #76 from buildkite-plugins/toote_avoid_duplicate_save
Browse files Browse the repository at this point in the history
Avoid duplicate save
  • Loading branch information
pzeballos authored Aug 7, 2024
2 parents 0c05633 + 1c72213 commit 280a02a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions hooks/post-command
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ configuration:
- zip
- tar
- tgz
force:
type: boolean
manifest:
type: string
path:
Expand Down
3 changes: 3 additions & 0 deletions tests/post-command-tgz.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions tests/post-command-zip.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 18 additions & 0 deletions tests/post-command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit 280a02a

Please sign in to comment.