Skip to content

Commit 6a57a0f

Browse files
authored
Merge pull request #276 from buildkite-plugins/toote_workdir_interpolation
Workdir interpolation
2 parents 7754095 + 689c740 commit 6a57a0f

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ Example: `[ "/var/run/docker.sock:/var/run/docker.sock" ]`
437437

438438
### `expand-volume-vars` (optional, boolean, run only, unsafe)
439439

440-
When set to true, it will activate interpolation of variables in the elements of the `volumes` configuration array. When turned off (the default), attempting to use variables will fail as the literal `$VARIABLE_NAME` string will be passed to the `-v` option.
440+
When set to true, it will activate interpolation of variables in the elements of the `volumes` configuration array as well as `workdir`. When turned off (the default), attempting to use variables will fail as the literal `$VARIABLE_NAME` string will be passed to the `-v` option.
441441

442442
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`.
443443

commands/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fi
6666
workdir=''
6767

6868
if [[ -n "${BUILDKITE_PLUGIN_DOCKER_WORKDIR:-}" ]] || [[ "${BUILDKITE_PLUGIN_DOCKER_MOUNT_CHECKOUT:-on}" =~ ^(true|on|1)$ ]] ; then
69-
workdir="${BUILDKITE_PLUGIN_DOCKER_WORKDIR:-$workdir_default}"
69+
workdir="$(expand_relative_volume_path "${BUILDKITE_PLUGIN_DOCKER_WORKDIR:-$workdir_default}")"
7070
fi
7171

7272
# By default, mount $PWD onto $WORKDIR

tests/command.bats

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,25 @@ setup() {
214214
unstub docker
215215
}
216216

217+
@test "Runs BUILDKITE_COMMAND with workdir with variables and option turned on" {
218+
# shellcheck disable=2016 # we want the variable not interpreted now
219+
export BUILDKITE_PLUGIN_DOCKER_WORKDIR='$ONE_VAR'
220+
export BUILDKITE_PLUGIN_DOCKER_EXPAND_VOLUME_VARS=true
221+
export BUILDKITE_COMMAND="pwd"
222+
223+
export ONE_VAR=/my/path
224+
225+
stub docker \
226+
"run -t -i --rm --init --volume $PWD:/my/path --workdir /my/path --label com.buildkite.job-id=1-2-3-4 image:tag /bin/sh -e -c 'pwd' : echo ran command in docker"
227+
228+
run "$PWD"/hooks/command
229+
230+
assert_success
231+
assert_output --partial "ran command in docker"
232+
233+
unstub docker
234+
}
235+
217236
@test "Runs BUILDKITE_COMMAND with devices" {
218237
export BUILDKITE_PLUGIN_DOCKER_WORKDIR=/app
219238
export BUILDKITE_PLUGIN_DOCKER_DEVICES_0=/dev/bus/usb/001/001

0 commit comments

Comments
 (0)