Skip to content

Commit 68e21f1

Browse files
authored
Merge pull request #111 from buildkite-plugins/toote_extra_key_data
Extra key data
2 parents 1c0ff20 + debb8cc commit 68e21f1

4 files changed

Lines changed: 123 additions & 1 deletion

File tree

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,34 @@ Remove compression artifacts after they are used. Default: `false`.
8888

8989
Note that if you turn on this option, every execution will create temporary files that may fill up your agent's storage.
9090

91+
### `key-extra`
92+
93+
Additional data used to compute the cache key. You can use this additional parameter to further differentiate within a given level.
94+
95+
For example when used in conjunction with a build matrix:
96+
97+
```yaml
98+
steps:
99+
- matrix:
100+
- "darwin"
101+
- "Linux"
102+
- "Windows"
103+
label: "{{matrix}} build"
104+
command: "GOOS={{matrix}} go build"
105+
env:
106+
os: "{{matrix}}"
107+
GOMODCACHE: pkg/cache
108+
plugins:
109+
- cache#v1.5.2:
110+
path: pkg/cache
111+
manifest:
112+
- go.mod
113+
- go.sum
114+
restore: file
115+
save: file
116+
key-extra: "{{matrix}}"
117+
```
118+
91119
### `manifest` (string or list of strings, required if using `file` caching level)
92120

93121
One or more paths to files or folders that will be hashed to create and restore file-level caches. If multiple files or folders are specified its ordering does not matter.
@@ -105,6 +133,8 @@ This plugin uses the following hierarchical structure for caches to be valid (me
105133

106134
When restoring from cache, **all levels, in the described order, up to the one specified** will be checked. The first one available will be restored and no further levels or checks will be made.
107135

136+
Note: you can use the [`key-extra`](#key-extra) option to further differentiate within a level
137+
108138
## Customizable backends
109139

110140
One of the greatest flexibilities of this plugin is its flexible backend architecture. You can provide whatever value you want for the `backend` option of this plugin (`X` for example) as long as there is an executable script accessible to the agent named `cache_X` that respects the following execution protocol:

lib/shared.bash

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ build_key() {
4040
local LEVEL="$1"
4141
local CACHE_PATH="$2"
4242
local COMPRESSION="${3:-}"
43+
local EXTRA="${BUILDKITE_PLUGIN_CACHE_KEY_EXTRA:-}"
4344

4445
if [ "${LEVEL}" = 'file' ]; then
4546
plugin_read_list_into_result MANIFEST
@@ -57,7 +58,7 @@ build_key() {
5758
exit 1
5859
fi
5960

60-
echo "cache-${LEVEL}-${BASE}-${CACHE_PATH}-${COMPRESSION}" | "$(sha)" | cut -d\ -f1
61+
echo "cache-${LEVEL}-${BASE}-${CACHE_PATH}-${COMPRESSION}${EXTRA}" | "$(sha)" | cut -d\ -f1
6162
}
6263

6364
backend_exec() {

plugin.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ configuration:
2121
type: boolean
2222
keep-compressed-artifacts:
2323
type: boolean
24+
key-extra:
25+
type: string
2426
manifest:
2527
oneOf:
2628
- type: string

tests/shared.bats

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,36 @@ teardown() {
9090
refute_output "${EMPTY_FILE}"
9191
assert_output "${MODIFIED_FILE}"
9292

93+
# using extra data changes the key
94+
export BUILDKITE_PLUGIN_CACHE_KEY_EXTRA=extra
95+
96+
run build_key file FOLDER
97+
EXTRA_KEY="${output}"
98+
99+
assert_success
100+
refute_output "${MODIFIED_FILE}"
101+
refute_output "${EMPTY_FILE}"
102+
103+
# different extra data changes the key
104+
export BUILDKITE_PLUGIN_CACHE_KEY_EXTRA=other_extra
105+
106+
run build_key file FOLDER
107+
108+
assert_success
109+
110+
refute_output "${MODIFIED_FILE}"
111+
refute_output "${EMPTY_FILE}"
112+
refute_output "${EXTRA_KEY}"
113+
114+
# empty extra data returns key to expected
115+
export BUILDKITE_PLUGIN_CACHE_KEY_EXTRA=''
116+
run build_key file FOLDER
117+
118+
assert_success
119+
assert_output "${MODIFIED_FILE}"
120+
refute_output "${EMPTY_FILE}"
121+
refute_output "${EXTRA_KEY}"
122+
93123
rm test.file
94124
rm -rf FOLDER
95125
rm other.file
@@ -131,6 +161,36 @@ teardown() {
131161
refute_output "${EMPTY_FOLDER}"
132162
assert_output "${MODIFIED_FOLDER}"
133163

164+
# using extra data changes the key
165+
export BUILDKITE_PLUGIN_CACHE_KEY_EXTRA=extra
166+
167+
run build_key file FOLDER
168+
EXTRA_KEY="${output}"
169+
170+
assert_success
171+
refute_output "${MODIFIED_FOLDER}"
172+
refute_output "${EMPTY_FOLDER}"
173+
174+
# different extra data changes the key
175+
export BUILDKITE_PLUGIN_CACHE_KEY_EXTRA=other_extra
176+
177+
run build_key file FOLDER
178+
179+
assert_success
180+
refute_output "${MODIFIED_FOLDER}"
181+
refute_output "${EMPTY_FOLDER}"
182+
refute_output "${EXTRA_KEY}"
183+
184+
# empty extra data returns key to expected
185+
export BUILDKITE_PLUGIN_CACHE_KEY_EXTRA=''
186+
187+
run build_key file FOLDER
188+
189+
assert_success
190+
assert_output "${MODIFIED_FOLDER}"
191+
refute_output "${EMPTY_FOLDER}"
192+
refute_output "${EXTRA_KEY}"
193+
134194
rm -rf test.folder
135195
rm -rf FOLDER
136196
}
@@ -153,4 +213,33 @@ teardown() {
153213
run build_key file FOLDER another_thing
154214
assert_success
155215
refute_output "${GENERATED_KEY}"
216+
217+
# extra data changes the key
218+
export BUILDKITE_PLUGIN_CACHE_KEY_EXTRA=extra
219+
220+
run build_key file FOLDER something
221+
EXTRA_KEY="${output}"
222+
223+
assert_success
224+
refute_output "${GENERATED_KEY}"
225+
refute_output "${EMPTY_KEY}"
226+
227+
# different extra data changes the key
228+
export BUILDKITE_PLUGIN_CACHE_KEY_EXTRA=other_extra
229+
run build_key file FOLDER something
230+
231+
assert_success
232+
refute_output "${GENERATED_KEY}"
233+
refute_output "${EMPTY_KEY}"
234+
refute_output "${EXTRA_KEY}"
235+
236+
# empty extra data returns key to expected
237+
export BUILDKITE_PLUGIN_CACHE_KEY_EXTRA=''
238+
run build_key file FOLDER something
239+
240+
assert_success
241+
assert_output "${GENERATED_KEY}"
242+
refute_output "${EMPTY_KEY}"
243+
refute_output "${EXTRA_KEY}"
244+
156245
}

0 commit comments

Comments
 (0)