Skip to content

Commit 0c05633

Browse files
authored
Merge pull request #75 from buildkite-plugins/toote_multiple_save_levels
Multiple save levels
2 parents e759854 + 12fd2be commit 0c05633

File tree

6 files changed

+123
-34
lines changed

6 files changed

+123
-34
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ The file or folder to cache.
3232

3333
The maximum caching level to restore, if available. See [the available caching levels](#caching-levels)
3434

35-
#### `save` (string, specific values)
35+
#### `save` (string or array of strings, specific values)
3636

37-
The level to use for saving the cache. See [the available caching levels](#caching-levels)
37+
The level(s) to use for saving the cache. See [the available caching levels](#caching-levels).
38+
39+
You can specify multiple levels in an array to save the same artifact as a cache for all those levels.
3840

3941
## Options
4042

@@ -118,7 +120,7 @@ You can always have more complicated logic by using the plugin multiple times wi
118120
* second step:
119121
- will restore the file-level cache of the `node_modules` folder saved by the first step and run `npm test`
120122
* third step (that will only run on the `master` branch):
121-
- will restore the file-level cache saved by the first step, run `npm run deploy` and finally save the contents of the `node_modules` folder as a pipeline-level cache for usage as a basis even when the lockfile changes (in the first step)
123+
- will restore the file-level cache saved by the first step, run `npm run deploy` and finally save the contents of the `node_modules` folder as both a pipeline-level and global (all-level) cache for usage as a basis even when the lockfile changes (in the first step)
122124

123125
```yaml
124126
steps:
@@ -147,7 +149,9 @@ steps:
147149
manifest: package-lock.json
148150
path: node_modules
149151
restore: file
150-
save: pipeline
152+
save:
153+
- pipeline
154+
- all
151155
152156
```
153157

hooks/post-command

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,29 @@ if [ -z "${CACHE_PATH}" ] ; then
1515
exit 1
1616
fi
1717

18-
LEVEL=$(plugin_read_config SAVE 'no')
19-
if [ "${LEVEL}" = 'no' ]; then
20-
echo 'Cache not setup for saving'
21-
exit 0
22-
elif [ "${LEVEL}" = 'file' ] && [ -z "$(plugin_read_config MANIFEST)" ]; then
23-
echo "+++ 🚨 Missing manifest option in the cache plugin for file-level saving"
24-
exit 1
25-
fi
26-
2718
COMPRESS=$(plugin_read_config COMPRESSION 'none')
2819
if ! validate_compression "${COMPRESS}"; then
2920
echo "+++ 🚨 Invalid value for compression option"
3021
exit 1
3122
fi
3223

33-
KEY=$(build_key "${LEVEL}" "${CACHE_PATH}" "${COMPRESS}")
24+
SAVE_LEVELS=()
25+
if plugin_read_list_into_result SAVE; then
26+
for LEVEL in "${result[@]}"; do
27+
SAVE_LEVELS+=("${LEVEL}")
28+
29+
# this validates the level as well
30+
KEY=$(build_key "${LEVEL}" "${CACHE_PATH}" "${COMPRESS}")
31+
32+
if [ "${LEVEL}" = 'file' ] && [ -z "$(plugin_read_config MANIFEST)" ]; then
33+
echo "+++ 🚨 Missing manifest option in the cache plugin for file-level saving"
34+
exit 1
35+
fi
36+
done
37+
else
38+
echo 'Cache not setup for saving'
39+
exit 0
40+
fi
3441

3542
if compression_active; then
3643
ACTUAL_PATH=$(mktemp)
@@ -39,5 +46,9 @@ else
3946
ACTUAL_PATH="${CACHE_PATH}"
4047
fi
4148

42-
echo "Saving ${LEVEL}-level cache of ${CACHE_PATH}"
43-
backend_exec save "${KEY}" "${ACTUAL_PATH}"
49+
for LEVEL in "${SAVE_LEVELS[@]}"; do
50+
KEY=$(build_key "${LEVEL}" "${CACHE_PATH}" "${COMPRESS}")
51+
52+
echo "Saving ${LEVEL}-level cache of ${CACHE_PATH}"
53+
backend_exec save "${KEY}" "${ACTUAL_PATH}"
54+
done

plugin.yml

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ description: Persist cache in between build steps
33
author: https://github.com/buildkite-plugins
44
requirements: []
55
configuration:
6+
$defs:
7+
valid_levels:
8+
type: string
9+
enum:
10+
- file
11+
- step
12+
- branch
13+
- pipeline
14+
- all
615
properties:
716
backend:
817
type: string
@@ -17,21 +26,15 @@ configuration:
1726
path:
1827
type: string
1928
restore:
20-
type: string
21-
enum:
22-
- file
23-
- step
24-
- branch
25-
- pipeline
26-
- all
29+
$ref: '#/$defs/valid_levels'
2730
save:
28-
type: string
29-
enum:
30-
- file
31-
- step
32-
- branch
33-
- pipeline
34-
- all
31+
oneOf:
32+
- $ref: '#/$defs/valid_levels'
33+
- type: array
34+
items:
35+
$ref: '#/$defs/valid_levels'
3536
additionalProperties: false
36-
required:
37-
- path
37+
anyOf:
38+
- required: [ path, save ]
39+
- required: [ path, restore ]
40+

tests/post-command-tgz.bats

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,19 @@ teardown() {
8585
assert_success
8686
assert_output --partial 'Saving all-level cache'
8787
assert_output --partial 'Compressing tests/data/my_files with tgz'
88-
}
88+
}
89+
90+
@test "Multiple level saving" {
91+
export BUILDKITE_PLUGIN_CACHE_SAVE_0=all
92+
export BUILDKITE_PLUGIN_CACHE_SAVE_1=pipeline
93+
94+
# add an extra save, but tar should still be called only once
95+
stub cache_dummy \
96+
"save \* \* : echo saving \$3 in \$2"
97+
98+
run "$PWD/hooks/post-command"
99+
100+
assert_success
101+
assert_output --partial 'Saving all-level cache'
102+
assert_output --partial 'Saving pipeline-level cache'
103+
}

tests/post-command-zip.bats

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,18 @@ teardown() {
8787
assert_output --partial 'Saving all-level cache'
8888
assert_output --partial 'Compressing tests/data/my_files with zip'
8989
}
90+
91+
@test "Multiple level saving" {
92+
export BUILDKITE_PLUGIN_CACHE_SAVE_0=all
93+
export BUILDKITE_PLUGIN_CACHE_SAVE_1=pipeline
94+
95+
# add an extra save, but zip should still be called only once
96+
stub cache_dummy \
97+
"save \* \* : echo saving \$3 in \$2"
98+
99+
run "$PWD/hooks/post-command"
100+
101+
assert_success
102+
assert_output --partial 'Saving all-level cache'
103+
assert_output --partial 'Saving pipeline-level cache'
104+
}

tests/post-command.bats

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,45 @@ teardown() {
137137
assert_output --partial 'Saving all-level cache'
138138

139139
unstub cache_dummy
140-
}
140+
}
141+
142+
@test "Multiple level saving" {
143+
export BUILDKITE_PLUGIN_CACHE_SAVE_0=all
144+
export BUILDKITE_PLUGIN_CACHE_SAVE_1=pipeline
145+
146+
stub cache_dummy \
147+
"save \* \* : echo saving \$3 in \$2" \
148+
"save \* \* : echo saving \$3 in \$2"
149+
150+
run "$PWD/hooks/post-command"
151+
152+
assert_success
153+
assert_output --partial 'Saving all-level cache'
154+
assert_output --partial 'Saving pipeline-level cache'
155+
156+
unstub cache_dummy
157+
}
158+
159+
@test "Multiple level file without manifest fails" {
160+
export BUILDKITE_PLUGIN_CACHE_SAVE_0=all
161+
export BUILDKITE_PLUGIN_CACHE_SAVE_1=file
162+
163+
run "$PWD/hooks/post-command"
164+
165+
assert_failure
166+
167+
assert_output --partial 'Missing manifest option'
168+
refute_output --partial 'Saving file-level cache'
169+
}
170+
171+
@test "Multiple level containing invalid one fails" {
172+
export BUILDKITE_PLUGIN_CACHE_SAVE_0=pipeline
173+
export BUILDKITE_PLUGIN_CACHE_SAVE_1=unreal
174+
175+
run "$PWD/hooks/post-command"
176+
177+
assert_failure
178+
179+
assert_output --partial 'Invalid cache level unreal'
180+
refute_output --partial 'Saving pipeline-level cache'
181+
}

0 commit comments

Comments
 (0)