Skip to content

Commit 280a02a

Browse files
authored
Merge pull request #76 from buildkite-plugins/toote_avoid_duplicate_save
Avoid duplicate save
2 parents 0c05633 + 1c72213 commit 280a02a

File tree

6 files changed

+40
-3
lines changed

6 files changed

+40
-3
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ Assuming the underlying executables are available, the allowed values are:
7373
* `tgz`: `tar` with gzip compression
7474
* `zip`: `(un)zip` compression
7575

76+
### `force` (boolean, optional, save only)
77+
78+
Force saving the cache even if it exists. Default: `false`.
79+
7680
### `manifest` (string, required if using `file` caching level)
7781

7882
A path to a file or folder that will be hashed to create file-level caches.
@@ -131,7 +135,9 @@ steps:
131135
manifest: package-lock.json
132136
path: node_modules
133137
restore: pipeline
134-
save: file
138+
save:
139+
- file
140+
- branch
135141
- wait: ~
136142
- label: ':test_tube: Run tests'
137143
command: npm test # does not save cache, not necessary

hooks/post-command

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ fi
4949
for LEVEL in "${SAVE_LEVELS[@]}"; do
5050
KEY=$(build_key "${LEVEL}" "${CACHE_PATH}" "${COMPRESS}")
5151

52-
echo "Saving ${LEVEL}-level cache of ${CACHE_PATH}"
53-
backend_exec save "${KEY}" "${ACTUAL_PATH}"
52+
if [ "$(plugin_read_config FORCE 'false')" != 'false' ] ||
53+
! backend_exec exists "${KEY}"; then
54+
echo "Saving ${LEVEL}-level cache of ${CACHE_PATH}"
55+
backend_exec save "${KEY}" "${ACTUAL_PATH}"
56+
else
57+
echo "Cache of ${LEVEL} already exists, skipping"
58+
fi
5459
done

plugin.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ configuration:
2121
- zip
2222
- tar
2323
- tgz
24+
force:
25+
type: boolean
2426
manifest:
2527
type: string
2628
path:

tests/post-command-tgz.bats

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ setup() {
1515
export BUILDKITE_PLUGIN_CACHE_COMPRESSION=tgz
1616
export BUILDKITE_PLUGIN_CACHE_PATH=tests/data/my_files
1717

18+
# to make all test easier
19+
export BUILDKITE_PLUGIN_CACHE_FORCE=true
20+
1821
# necessary for key-calculations
1922
export BUILDKITE_LABEL="step-label"
2023
export BUILDKITE_BRANCH="tests"

tests/post-command-zip.bats

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ setup() {
1515
export BUILDKITE_PLUGIN_CACHE_COMPRESSION=zip
1616
export BUILDKITE_PLUGIN_CACHE_PATH=tests/data/my_files
1717

18+
# to make all test easier
19+
export BUILDKITE_PLUGIN_CACHE_FORCE=true
20+
1821
# necessary for key-calculations
1922
export BUILDKITE_LABEL="step-label"
2023
export BUILDKITE_BRANCH="tests"

tests/post-command.bats

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ setup() {
1313
export BUILDKITE_PLUGIN_CACHE_BACKEND=dummy
1414
export BUILDKITE_PLUGIN_CACHE_PATH=tests/data/my_files
1515

16+
# to make all test easier
17+
export BUILDKITE_PLUGIN_CACHE_FORCE=true
18+
1619
# necessary for key-calculations
1720
export BUILDKITE_LABEL="step-label"
1821
export BUILDKITE_BRANCH="tests"
@@ -179,3 +182,18 @@ teardown() {
179182
assert_output --partial 'Invalid cache level unreal'
180183
refute_output --partial 'Saving pipeline-level cache'
181184
}
185+
186+
@test "Saving is skipped when cache exists" {
187+
export BUILDKITE_PLUGIN_CACHE_SAVE=all
188+
export BUILDKITE_PLUGIN_CACHE_FORCE='false'
189+
190+
stub cache_dummy \
191+
"exists \* \* : exit 0"
192+
193+
run "$PWD/hooks/post-command"
194+
195+
assert_success
196+
refute_output --partial 'Saving all-level cache'
197+
198+
unstub cache_dummy
199+
}

0 commit comments

Comments
 (0)