Skip to content

Commit f8e70fb

Browse files
authored
Add support to cache on specific json keys (#51)
* Add support to cache on specific json keys * Tweaks * add nested access * bump versions * nit * Tweak readme * tweak * tweak * sort * phrasing
1 parent 5560666 commit f8e70fb

File tree

4 files changed

+88
-19
lines changed

4 files changed

+88
-19
lines changed

.prettierrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"singleQuote": true
3+
}

README.md

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ RUN echo 'my expensive build step'
2727
steps:
2828
- command: echo wow
2929
plugins:
30-
- seek-oss/docker-ecr-cache#v2.1.1
30+
- seek-oss/docker-ecr-cache#v2.2.0
3131
- docker#v3.12.0
3232
```
3333
@@ -52,7 +52,7 @@ RUN npm install
5252
steps:
5353
- command: npm test
5454
plugins:
55-
- seek-oss/docker-ecr-cache#v2.1.1:
55+
- seek-oss/docker-ecr-cache#v2.2.0:
5656
cache-on:
5757
- package.json # avoid cache hits on stale lockfiles
5858
- package-lock.json
@@ -67,7 +67,7 @@ The `cache-on` property also supports Bash globbing with `globstar`:
6767
steps:
6868
- command: npm test
6969
plugins:
70-
- seek-oss/docker-ecr-cache#v2.1.1:
70+
- seek-oss/docker-ecr-cache#v2.2.0:
7171
cache-on:
7272
- '**/package.json' # monorepo with multiple manifest files
7373
- yarn.lock
@@ -76,6 +76,27 @@ steps:
7676
- /workdir/node_modules
7777
```
7878

79+
It also supports caching on specific JSON keys which can be specified following a `#` character using [jq syntax](https://jqlang.github.io/jq/manual/#object-identifier-index). This requires [jq](https://jqlang.github.io/jq/) to be installed on the build agent. This implementation works by matching on the first `.json#` substring.
80+
81+
A given entry cannot contain both bash globbing and a jq path.
82+
83+
```yaml
84+
steps:
85+
- command: pnpm test
86+
plugins:
87+
- seek-oss/docker-ecr-cache#v2.2.0:
88+
cache-on:
89+
- .npmrc
90+
- package.json#.dependencies
91+
- package.json#.devDependencies
92+
- package.json#.packageManager
93+
- package.json#.pnpm.overrides
94+
- pnpm-lock.yaml
95+
- docker#v3.12.0:
96+
volumes:
97+
- /workdir/node_modules
98+
```
99+
79100
### Using another Dockerfile
80101

81102
It's possible to specify the Dockerfile to use by:
@@ -84,7 +105,7 @@ It's possible to specify the Dockerfile to use by:
84105
steps:
85106
- command: echo wow
86107
plugins:
87-
- seek-oss/docker-ecr-cache#v2.1.1:
108+
- seek-oss/docker-ecr-cache#v2.2.0:
88109
dockerfile: my-dockerfile
89110
- docker#v3.12.0
90111
```
@@ -95,7 +116,7 @@ Alternatively, Dockerfile can be embedded inline:
95116
steps:
96117
- command: echo wow
97118
plugins:
98-
- seek-oss/docker-ecr-cache#v2.1.1:
119+
- seek-oss/docker-ecr-cache#v2.2.0:
99120
dockerfile-inline: |
100121
FROM node:16-alpine
101122
WORKDIR /workdir
@@ -124,7 +145,7 @@ steps:
124145
--build-arg BUILDKITE_PLUGIN_DOCKER_ECR_CACHE_EXPORT_TAG
125146
--file Dockerfile.secondary
126147
plugins:
127-
- seek-oss/docker-ecr-cache#v2.1.1
148+
- seek-oss/docker-ecr-cache#v2.2.0
128149
```
129150

130151
Your `Dockerfile.secondary` can then [dynamically use these args](https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact):
@@ -152,7 +173,7 @@ stage to run commands against:
152173
steps:
153174
- command: cargo test
154175
plugins:
155-
- seek-oss/docker-ecr-cache#v2.1.1:
176+
- seek-oss/docker-ecr-cache#v2.2.0:
156177
target: build-deps
157178
- docker#v3.12.0
158179
```
@@ -167,7 +188,7 @@ The `context` property can be used to specify a different path.
167188
steps:
168189
- command: cargo test
169190
plugins:
170-
- seek-oss/docker-ecr-cache#v2.1.1:
191+
- seek-oss/docker-ecr-cache#v2.2.0:
171192
dockerfile: dockerfiles/test/Dockerfile
172193
context: '.'
173194
- docker#v3.12.0
@@ -196,7 +217,7 @@ steps:
196217
env:
197218
ARG_1: wow
198219
plugins:
199-
- seek-oss/docker-ecr-cache#v2.1.1:
220+
- seek-oss/docker-ecr-cache#v2.2.0:
200221
build-args:
201222
- ARG_1
202223
- ARG_2=such
@@ -211,7 +232,7 @@ steps:
211232
env:
212233
ARG_1: wow
213234
plugins:
214-
- seek-oss/docker-ecr-cache#v2.1.1:
235+
- seek-oss/docker-ecr-cache#v2.2.0:
215236
additional-build-args: '--ssh= default=\$SSH_AUTH_SOCK'
216237
- docker#v3.12.0
217238
```
@@ -239,7 +260,7 @@ steps:
239260
env:
240261
SECRET: wow
241262
plugins:
242-
- seek-oss/docker-ecr-cache#v2.1.1:
263+
- seek-oss/docker-ecr-cache#v2.2.0:
243264
secrets:
244265
- SECRET
245266
- docker#v3.12.0
@@ -255,7 +276,7 @@ steps:
255276
plugins:
256277
- seek-oss/private-npm#v1.2.0:
257278
env: SECRET
258-
- seek-oss/docker-ecr-cache#v2.1.1:
279+
- seek-oss/docker-ecr-cache#v2.2.0:
259280
secrets:
260281
- id=npmrc,src=.npmrc
261282
- docker#v3.12.0
@@ -273,7 +294,7 @@ By default images are kept in ECR for up to 30 days. This can be changed by spec
273294
steps:
274295
- command: echo wow
275296
plugins:
276-
- seek-oss/docker-ecr-cache#v2.1.1:
297+
- seek-oss/docker-ecr-cache#v2.2.0:
277298
max-age-days: 7
278299
- docker#v3.12.0
279300
```
@@ -286,7 +307,7 @@ By default, image name and computed tag are exported to the Docker buildkite plu
286307
steps:
287308
- command: echo wow
288309
plugins:
289-
- seek-oss/docker-ecr-cache#v2.1.1:
310+
- seek-oss/docker-ecr-cache#v2.2.0:
290311
export-env-variable: BUILDKITE_PLUGIN_MY_CUSTOM_PLUGIN_CACHE_IMAGE
291312
- my-custom-plugin#v1.0.0:
292313
```
@@ -300,7 +321,7 @@ steps:
300321
- label: Build Cache
301322
command: ':'
302323
plugins:
303-
- seek-oss/docker-ecr-cache#v2.1.1:
324+
- seek-oss/docker-ecr-cache#v2.2.0:
304325
skip-pull-from-cache: true
305326
```
306327

@@ -316,7 +337,7 @@ optionally use a custom repository name:
316337
steps:
317338
- command: echo wow
318339
plugins:
319-
- seek-oss/docker-ecr-cache#v2.1.1:
340+
- seek-oss/docker-ecr-cache#v2.2.0:
320341
ecr-name: my-unique-repository-name
321342
ecr-tags:
322343
Key: Value
@@ -332,7 +353,7 @@ By default, the plugin uses the region specified in the `AWS_DEFAULT_REGION` env
332353
steps:
333354
- command: echo wow
334355
plugins:
335-
- seek-oss/docker-ecr-cache#v2.1.1:
356+
- seek-oss/docker-ecr-cache#v2.2.0:
336357
region: ap-southeast-2
337358
- docker#v3.12.0
338359
```
@@ -374,7 +395,7 @@ Example:
374395
```yaml
375396
- command: echo wow
376397
plugins:
377-
- seek-oss/docker-ecr-cache#v2.1.1:
398+
- seek-oss/docker-ecr-cache#v2.2.0:
378399
registry-provider: gcr
379400
gcp-project: foo-bar-123456
380401
```

hooks/lib/stdlib.bash

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,18 @@ compute_tag() {
100100
echoerr "${glob}"
101101
for file in ${glob}; do
102102
echoerr "+ ${file}"
103-
sums+="$(sha1sum "${file}")"
103+
if [[ "${file}" == *.json#* ]]; then
104+
# Extract the file path and keys from the pattern
105+
file_path="${file%%#*}"
106+
keys=${file#*#}
107+
108+
# Read the JSON file and calculate sha1sum only for the specified keys
109+
value=$(jq -r "${keys}" "${file_path}")
110+
sums+="$(echo -n "${value}" | sha1sum)"
111+
else
112+
# Calculate sha1sum for the whole file
113+
sums+="$(sha1sum "${file}")"
114+
fi
104115
done
105116
done
106117

tests/stdlib.bats

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# export UNAME_STUB_DEBUG=/dev/tty
44
# export SHA1SUM_STUB_DEBUG=/dev/tty
5+
# export JQ_STUB_DEBUG=/dev/tty
56

67
load "$BATS_PLUGIN_PATH/load.bash"
78
load "$PWD/hooks/lib/stdlib.bash"
@@ -159,3 +160,36 @@ pre_command_hook="$PWD/hooks/pre-command"
159160
unstub uname
160161
unstub sha1sum
161162
}
163+
164+
@test "Can compute image tag with cache-on on specific json keys" {
165+
# this var leaks in via pre-command
166+
target="my-multi-stage-container"
167+
export BUILDKITE_PLUGIN_DOCKER_ECR_CACHE_CACHE_ON_1="test-package.json#.dependencies"
168+
169+
stub uname \
170+
"-m : echo my-architecture" \
171+
"-m : echo my-architecture"
172+
stub jq \
173+
"-r .dependencies test-package.json : echo '{\"test\":\"123\"}'"
174+
stub sha1sum \
175+
"pretend-dockerfile : echo sha1sum(pretend-dockerfile)" \
176+
": echo sha1sum(target: my-multi-stage-container)" \
177+
": echo sha1sum(uname: my-architecture)" \
178+
": echo sha1sum(jq: .dependencies)" \
179+
": echo sha1sum(hashes so far)"
180+
181+
run compute_tag "pretend-dockerfile"
182+
183+
assert_success
184+
assert_line "--- Computing tag"
185+
assert_line "DOCKERFILE"
186+
assert_line "+ pretend-dockerfile:my-multi-stage-container"
187+
assert_line "ARCHITECTURE"
188+
assert_line "+ my-architecture"
189+
assert_line "BUILD_ARGS"
190+
assert_line "CACHE_ON"
191+
192+
unstub uname
193+
unstub jq
194+
unstub sha1sum
195+
}

0 commit comments

Comments
 (0)