Skip to content

Commit 843c9f7

Browse files
authored
Merge pull request #50 from buildkite-plugins/add-interpreter-option
Add a shell option
2 parents a15a53f + 09ff032 commit 843c9f7

File tree

4 files changed

+101
-7
lines changed

4 files changed

+101
-7
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,15 @@ Specify an explicit docker runtime. See the [docker run options documentation](h
120120

121121
Example: `nvidia`
122122

123+
### `shell` (optional)
124+
125+
Set the shell to use for the command. Set it to `false` to pass the command directly to the `docker run` command. The default is `bash -c`.
126+
127+
Example: `powershell -Command`
128+
123129
### `entrypoint` (optional)
124130

125-
Override the image’s default entrypoint. See the [docker run --entrypoint documentation](https://docs.docker.com/engine/reference/run/#entrypoint-default-command-to-execute-at-runtime) for more details.
131+
Override the image’s default entrypoint, and defaults the `shell` option to `false`. See the [docker run --entrypoint documentation](https://docs.docker.com/engine/reference/run/#entrypoint-default-command-to-execute-at-runtime) for more details.
126132

127133
Example: `/my/custom/entrypoint.sh`
128134

hooks/command

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,29 @@ fi
8181
# Support docker run --entrypoint
8282
if [[ -n "${BUILDKITE_PLUGIN_DOCKER_ENTRYPOINT:-}" ]] ; then
8383
args+=("--entrypoint" "${BUILDKITE_PLUGIN_DOCKER_ENTRYPOINT:-}")
84+
BUILDKITE_PLUGIN_DOCKER_SHELL="${BUILDKITE_PLUGIN_DOCKER_SHELL:-false}"
85+
fi
86+
87+
BUILDKITE_PLUGIN_DOCKER_SHELL="${BUILDKITE_PLUGIN_DOCKER_SHELL:-bash -c}"
88+
89+
if [[ "${BUILDKITE_PLUGIN_DOCKER_SHELL}" =~ ^(false|off|0)$ ]] ; then
90+
BUILDKITE_PLUGIN_DOCKER_SHELL=''
91+
fi
92+
93+
args+=("${BUILDKITE_PLUGIN_DOCKER_IMAGE}")
94+
95+
if [[ ! -z "${BUILDKITE_PLUGIN_DOCKER_SHELL:-}" ]]; then
96+
# shellcheck disable=SC2206 # We want the word splits
97+
args+=(${BUILDKITE_PLUGIN_DOCKER_SHELL} "${BUILDKITE_COMMAND}")
98+
else
99+
# shellcheck disable=SC2206 # We want the word splits
100+
args+=(${BUILDKITE_COMMAND})
84101
fi
85102

86103
echo "--- :docker: Running ${BUILDKITE_COMMAND} in ${BUILDKITE_PLUGIN_DOCKER_IMAGE}"
87104

88105
if [[ "${debug_mode:-off}" =~ (on) ]] ; then
89-
printf "$ docker run ${args[*]} %q %q" \
90-
"${BUILDKITE_PLUGIN_DOCKER_IMAGE}" \
91-
"${BUILDKITE_COMMAND}" >&2
106+
echo "$ docker run ${args[*]}" >&2
92107
fi
93108

94-
docker run "${args[@]}" "${BUILDKITE_PLUGIN_DOCKER_IMAGE}" bash -c "${BUILDKITE_COMMAND}"
109+
docker run "${args[@]}"

plugin.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ configuration:
2929
type: boolean
3030
runtime:
3131
type: string
32+
shell:
33+
type: string
3234
required:
3335
- image
3436
additionalProperties: false

tests/command.bats

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,15 @@ load '/usr/local/lib/bats/load.bash'
259259
unset BUILDKITE_COMMAND
260260
}
261261

262-
@test "Runs with entrypoint option" {
262+
@test "Runs with entrypoint option w/o shell by default" {
263263
export BUILDKITE_PLUGIN_DOCKER_WORKDIR=/app
264264
export BUILDKITE_PLUGIN_DOCKER_IMAGE=image:tag
265265
export BUILDKITE_PLUGIN_DOCKER_MOUNT_BUILDKITE_AGENT=false
266266
export BUILDKITE_PLUGIN_DOCKER_ENTRYPOINT=/some/custom/entry/point
267267
export BUILDKITE_COMMAND="echo hello world"
268268

269269
stub docker \
270-
"run -it --rm --volume $PWD:/app --workdir /app --entrypoint /some/custom/entry/point image:tag bash -c 'echo hello world' : echo ran command in docker"
270+
"run -it --rm --volume $PWD:/app --workdir /app --entrypoint /some/custom/entry/point image:tag echo hello world : echo ran command in docker"
271271

272272
run $PWD/hooks/command
273273

@@ -281,3 +281,74 @@ load '/usr/local/lib/bats/load.bash'
281281
unset BUILDKITE_PLUGIN_DOCKER_ENTRYPOINT
282282
unset BUILDKITE_COMMAND
283283
}
284+
285+
@test "Runs with entrypoint option w/ explicit shell" {
286+
export BUILDKITE_PLUGIN_DOCKER_WORKDIR=/app
287+
export BUILDKITE_PLUGIN_DOCKER_IMAGE=image:tag
288+
export BUILDKITE_PLUGIN_DOCKER_MOUNT_BUILDKITE_AGENT=false
289+
export BUILDKITE_PLUGIN_DOCKER_ENTRYPOINT=/some/custom/entry/point
290+
export BUILDKITE_PLUGIN_DOCKER_SHELL='custom-bash -a -b'
291+
export BUILDKITE_COMMAND="echo hello world"
292+
293+
stub docker \
294+
"run -it --rm --volume $PWD:/app --workdir /app --entrypoint /some/custom/entry/point image:tag custom-bash -a -b 'echo hello world' : echo ran command in docker"
295+
296+
run $PWD/hooks/command
297+
298+
assert_success
299+
assert_output --partial "ran command in docker"
300+
301+
unstub docker
302+
unset BUILDKITE_PLUGIN_DOCKER_WORKDIR
303+
unset BUILDKITE_PLUGIN_DOCKER_IMAGE
304+
unset BUILDKITE_PLUGIN_DOCKER_MOUNT_BUILDKITE_AGENT
305+
unset BUILDKITE_PLUGIN_DOCKER_ENTRYPOINT
306+
unset BUILDKITE_PLUGIN_DOCKER_SHELL
307+
unset BUILDKITE_COMMAND
308+
}
309+
310+
@test "Runs with shell option" {
311+
export BUILDKITE_PLUGIN_DOCKER_WORKDIR=/app
312+
export BUILDKITE_PLUGIN_DOCKER_IMAGE=image:tag
313+
export BUILDKITE_PLUGIN_DOCKER_MOUNT_BUILDKITE_AGENT=false
314+
export BUILDKITE_PLUGIN_DOCKER_SHELL='custom-bash -a -b'
315+
export BUILDKITE_COMMAND="echo hello world"
316+
317+
stub docker \
318+
"run -it --rm --volume $PWD:/app --workdir /app image:tag custom-bash -a -b 'echo hello world' : echo ran command in docker"
319+
320+
run $PWD/hooks/command
321+
322+
assert_success
323+
assert_output --partial "ran command in docker"
324+
325+
unstub docker
326+
unset BUILDKITE_PLUGIN_DOCKER_WORKDIR
327+
unset BUILDKITE_PLUGIN_DOCKER_IMAGE
328+
unset BUILDKITE_PLUGIN_DOCKER_MOUNT_BUILDKITE_AGENT
329+
unset BUILDKITE_PLUGIN_DOCKER_SHELL
330+
unset BUILDKITE_COMMAND
331+
}
332+
333+
@test "Runs with shell disabled" {
334+
export BUILDKITE_PLUGIN_DOCKER_WORKDIR=/app
335+
export BUILDKITE_PLUGIN_DOCKER_IMAGE=image:tag
336+
export BUILDKITE_PLUGIN_DOCKER_MOUNT_BUILDKITE_AGENT=false
337+
export BUILDKITE_PLUGIN_DOCKER_SHELL=false
338+
export BUILDKITE_COMMAND="echo hello world"
339+
340+
stub docker \
341+
"run -it --rm --volume $PWD:/app --workdir /app image:tag echo hello world : echo ran command in docker"
342+
343+
run $PWD/hooks/command
344+
345+
assert_success
346+
assert_output --partial "ran command in docker"
347+
348+
unstub docker
349+
unset BUILDKITE_PLUGIN_DOCKER_WORKDIR
350+
unset BUILDKITE_PLUGIN_DOCKER_IMAGE
351+
unset BUILDKITE_PLUGIN_DOCKER_MOUNT_BUILDKITE_AGENT
352+
unset BUILDKITE_PLUGIN_DOCKER_SHELL
353+
unset BUILDKITE_COMMAND
354+
}

0 commit comments

Comments
 (0)