Skip to content

Commit d16d4e6

Browse files
committed
Support secrets for building the cached image
1 parent 26a1494 commit d16d4e6

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,37 @@ steps:
167167
- docker#v3.3.0
168168
```
169169

170+
### Specifying secrets
171+
172+
[Build-time variables] can be extracted from a pulled image, so when passing
173+
sensitive data, [secrets] should be used instead.
174+
175+
[secrets]: https://docs.docker.com/develop/develop-images/build_enhancements/#new-docker-build-secret-information
176+
177+
To use environment variables (perhaps fetched by another plugin) as secrets:
178+
179+
```dockerfile
180+
# syntax=docker/dockerfile:1.2
181+
182+
FROM bash
183+
184+
RUN --mount=type=secret,id=SECRET cat /run/secrets/SECRET
185+
```
186+
187+
```yaml
188+
steps:
189+
- command: echo amaze
190+
env:
191+
SECRET: wow
192+
plugins:
193+
- seek-oss/docker-ecr-cache#v1.9.0:
194+
secrets:
195+
- SECRET
196+
- docker#v3.3.0
197+
```
198+
199+
You must have a recent version of Docker with BuildKit enabled to use secrets.
200+
170201
### Changing the max cache time
171202

172203
By default images are kept in ECR for up to 30 days. This can be changed by specifying a `max-age-days` parameter:

hooks/lib/stdlib.bash

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ read_build_args() {
2020
done
2121
}
2222

23+
read_secrets() {
24+
read_list_property 'SECRETS'
25+
for arg in ${result[@]+"${result[@]}"}; do
26+
secrets_args+=("--secret")
27+
secrets_args+=("id=${arg},env=${arg}")
28+
done
29+
}
30+
2331
# read a plugin property of type [array, string] into a Bash array. Buildkite
2432
# exposes a string value at BUILDKITE_PLUGIN_{NAME}_{KEY}, and array values at
2533
# BUILDKITE_PLUGIN_{NAME}_{KEY}_{IDX}.

hooks/pre-command

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ context="${BUILDKITE_PLUGIN_DOCKER_ECR_CACHE_CONTEXT:-"${docker_file_dir}"}"
2020
build_args=()
2121
read_build_args
2222

23+
secrets_args=()
24+
read_secrets
25+
2326
echo "--- Pulling image"
2427
if ! docker pull "${image}:${tag}"; then
2528
echo '--- Building image'
@@ -40,6 +43,13 @@ if ! docker pull "${image}:${tag}"; then
4043
)
4144
done
4245
fi
46+
if [[ "${#secrets_args[@]}" -gt 0 ]]; then
47+
for sa in "${secrets_args[@]}"; do
48+
image_build_args+=(
49+
"${sa}"
50+
)
51+
done
52+
fi
4353

4454
echo "Inside $(pwd), running \`docker ${image_build_args[*]} ${context}\`"
4555
# We can't quote BUILDKITE_PLUGIN_DOCKER_ECR_CACHE_ADDITIONAL_BUILD_ARGS, because it's passed here as a string instead of a bash array.

0 commit comments

Comments
 (0)