Skip to content

Commit cb79ad4

Browse files
authored
Merge pull request #246 from amartani/martani/ecr-variables
Allow expanding environment variables in image name
2 parents 7cc78ed + cbbc4f3 commit cb79ad4

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@ An array of additional files to pass into to the docker container as environment
181181

182182
If you set this to `VALUE`, and `VALUE` is an environment variable containing a space-separated list of environment variables such as `A B C D`, then A, B, C, and D will all be propagated to the container. This is helpful when you've set up an `environment` hook to export secrets as environment variables, and you'd also like to programmatically ensure that secrets get propagated to containers, instead of listing them all out.
183183

184+
### `expand-image-vars` (optional, boolean, unsafe)
185+
186+
When set to true, it will activate interpolation of variables in the elements of the `image` configuration variable. When turned off (the default), attempting to use variables will fail as the literal `$VARIABLE_NAME` string will be passed as the image name.
187+
188+
Environment variable interporation rules apply here. `$VARIABLE_NAME` is resolved at pipeline upload time, whereas `$$VARIABLE_NAME` is at run time. All things being equal, you likely want `$$VARIABLE_NAME`.
189+
190+
:warning: **Important:** this is considered an unsafe option as the most compatible way to achieve this is to run the strings through `eval` which could lead to arbitrary code execution or information leaking if you don't have complete control of the pipeline
191+
184192
### `propagate-environment` (optional, boolean)
185193

186194
Whether or not to automatically propagate all* pipeline environment variables into the docker container. Avoiding the need to be specified with `environment`.

hooks/command

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,16 @@ if [[ "${BUILDKITE_PLUGIN_DOCKER_PROPAGATE_AWS_AUTH_TOKENS:-false}" =~ ^(true|on
365365
fi
366366
fi
367367

368+
if [[ "${BUILDKITE_PLUGIN_DOCKER_EXPAND_IMAGE_VARS:-false}" =~ ^(true|on|1)$ ]] ; then
369+
image=$(eval echo "${BUILDKITE_PLUGIN_DOCKER_IMAGE}")
370+
else
371+
image="${BUILDKITE_PLUGIN_DOCKER_IMAGE}"
372+
fi
373+
368374
if [[ "${BUILDKITE_PLUGIN_DOCKER_ALWAYS_PULL:-false}" =~ ^(true|on|1)$ ]] ; then
369-
echo "--- :docker: Pulling ${BUILDKITE_PLUGIN_DOCKER_IMAGE}"
375+
echo "--- :docker: Pulling ${image}"
370376
if ! retry "${BUILDKITE_PLUGIN_DOCKER_PULL_RETRIES:-3}" \
371-
docker pull "${BUILDKITE_PLUGIN_DOCKER_IMAGE}" ; then
377+
docker pull "${image}" ; then
372378
echo "!!! :docker: Pull failed."
373379
exit "$retry_exit_status"
374380
fi
@@ -487,7 +493,7 @@ fi
487493
args+=("--label" "com.buildkite.job-id=${BUILDKITE_JOB_ID}")
488494

489495
# Add the image in before the shell and command
490-
args+=("${BUILDKITE_PLUGIN_DOCKER_IMAGE}")
496+
args+=("${image}")
491497

492498
# Set a default shell if one is needed
493499
if [[ -z $shell_disabled ]] && [[ ${#shell[@]} -eq 0 ]] ; then
@@ -534,7 +540,7 @@ elif [[ ${#command[@]} -gt 0 ]] ; then
534540
done
535541
fi
536542

537-
echo "--- :docker: Running command in ${BUILDKITE_PLUGIN_DOCKER_IMAGE}"
543+
echo "--- :docker: Running command in ${image}"
538544
echo -ne '\033[90m$\033[0m docker run ' >&2
539545

540546
# Print all the arguments, with a space after, properly shell quoted

plugin.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ configuration:
2525
type: array
2626
env-propagation-list:
2727
type: string
28+
expand-image-vars:
29+
type: boolean
2830
image:
2931
type: string
3032
ipc:

tests/command.bats

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,3 +1242,36 @@ EOF
12421242

12431243
unstub docker
12441244
}
1245+
1246+
@test "Do not expand image vars by default" {
1247+
export BUILDKITE_PLUGIN_DOCKER_IMAGE='123456789012.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/image:tag'
1248+
export AWS_DEFAULT_REGION="us-east-1"
1249+
export BUILDKITE_COMMAND="pwd"
1250+
1251+
stub docker \
1252+
"run -t -i --rm --init --volume $PWD:/workdir --workdir /workdir --label com.buildkite.job-id=1-2-3-4 123456789012.dkr.ecr.\$\{AWS_DEFAULT_REGION\}.amazonaws.com/image:tag /bin/sh -e -c 'pwd' : echo ran command in docker"
1253+
1254+
run "$PWD"/hooks/command
1255+
1256+
assert_success
1257+
assert_output --partial "ran command in docker"
1258+
1259+
unstub docker
1260+
}
1261+
1262+
@test "Expand image vars" {
1263+
export BUILDKITE_PLUGIN_DOCKER_EXPAND_IMAGE_VARS=true
1264+
export BUILDKITE_PLUGIN_DOCKER_IMAGE='123456789012.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/image:tag'
1265+
export AWS_DEFAULT_REGION="us-east-1"
1266+
export BUILDKITE_COMMAND="pwd"
1267+
1268+
stub docker \
1269+
"run -t -i --rm --init --volume $PWD:/workdir --workdir /workdir --label com.buildkite.job-id=1-2-3-4 123456789012.dkr.ecr.us-east-1.amazonaws.com/image:tag /bin/sh -e -c 'pwd' : echo ran command in docker"
1270+
1271+
run "$PWD"/hooks/command
1272+
1273+
assert_success
1274+
assert_output --partial "ran command in docker"
1275+
1276+
unstub docker
1277+
}

0 commit comments

Comments
 (0)