diff --git a/README.md b/README.md index c698223..489059b 100644 --- a/README.md +++ b/README.md @@ -120,9 +120,15 @@ Specify an explicit docker runtime. See the [docker run options documentation](h Example: `nvidia` +### `shell` (optional) + +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`. + +Example: `powershell -Command` + ### `entrypoint` (optional) -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. +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. Example: `/my/custom/entrypoint.sh` diff --git a/hooks/command b/hooks/command index b25781d..25dd571 100755 --- a/hooks/command +++ b/hooks/command @@ -81,14 +81,29 @@ fi # Support docker run --entrypoint if [[ -n "${BUILDKITE_PLUGIN_DOCKER_ENTRYPOINT:-}" ]] ; then args+=("--entrypoint" "${BUILDKITE_PLUGIN_DOCKER_ENTRYPOINT:-}") + BUILDKITE_PLUGIN_DOCKER_SHELL="${BUILDKITE_PLUGIN_DOCKER_SHELL:-false}" +fi + +BUILDKITE_PLUGIN_DOCKER_SHELL="${BUILDKITE_PLUGIN_DOCKER_SHELL:-bash -c}" + +if [[ "${BUILDKITE_PLUGIN_DOCKER_SHELL}" =~ ^(false|off|0)$ ]] ; then + BUILDKITE_PLUGIN_DOCKER_SHELL='' +fi + +args+=("${BUILDKITE_PLUGIN_DOCKER_IMAGE}") + +if [[ ! -z "${BUILDKITE_PLUGIN_DOCKER_SHELL:-}" ]]; then + # shellcheck disable=SC2206 # We want the word splits + args+=(${BUILDKITE_PLUGIN_DOCKER_SHELL} "${BUILDKITE_COMMAND}") +else + # shellcheck disable=SC2206 # We want the word splits + args+=(${BUILDKITE_COMMAND}) fi echo "--- :docker: Running ${BUILDKITE_COMMAND} in ${BUILDKITE_PLUGIN_DOCKER_IMAGE}" if [[ "${debug_mode:-off}" =~ (on) ]] ; then - printf "$ docker run ${args[*]} %q %q" \ - "${BUILDKITE_PLUGIN_DOCKER_IMAGE}" \ - "${BUILDKITE_COMMAND}" >&2 + echo "$ docker run ${args[*]}" >&2 fi -docker run "${args[@]}" "${BUILDKITE_PLUGIN_DOCKER_IMAGE}" bash -c "${BUILDKITE_COMMAND}" +docker run "${args[@]}" \ No newline at end of file diff --git a/plugin.yml b/plugin.yml index 56fc0d8..4f4c495 100644 --- a/plugin.yml +++ b/plugin.yml @@ -29,6 +29,8 @@ configuration: type: boolean runtime: type: string + shell: + type: string required: - image additionalProperties: false diff --git a/tests/command.bats b/tests/command.bats index 5e62acd..6fa4dab 100644 --- a/tests/command.bats +++ b/tests/command.bats @@ -259,7 +259,7 @@ load '/usr/local/lib/bats/load.bash' unset BUILDKITE_COMMAND } -@test "Runs with entrypoint option" { +@test "Runs with entrypoint option w/o shell by default" { export BUILDKITE_PLUGIN_DOCKER_WORKDIR=/app export BUILDKITE_PLUGIN_DOCKER_IMAGE=image:tag export BUILDKITE_PLUGIN_DOCKER_MOUNT_BUILDKITE_AGENT=false @@ -267,7 +267,7 @@ load '/usr/local/lib/bats/load.bash' export BUILDKITE_COMMAND="echo hello world" stub docker \ - "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" + "run -it --rm --volume $PWD:/app --workdir /app --entrypoint /some/custom/entry/point image:tag echo hello world : echo ran command in docker" run $PWD/hooks/command @@ -281,3 +281,74 @@ load '/usr/local/lib/bats/load.bash' unset BUILDKITE_PLUGIN_DOCKER_ENTRYPOINT unset BUILDKITE_COMMAND } + +@test "Runs with entrypoint option w/ explicit shell" { + export BUILDKITE_PLUGIN_DOCKER_WORKDIR=/app + export BUILDKITE_PLUGIN_DOCKER_IMAGE=image:tag + export BUILDKITE_PLUGIN_DOCKER_MOUNT_BUILDKITE_AGENT=false + export BUILDKITE_PLUGIN_DOCKER_ENTRYPOINT=/some/custom/entry/point + export BUILDKITE_PLUGIN_DOCKER_SHELL='custom-bash -a -b' + export BUILDKITE_COMMAND="echo hello world" + + stub docker \ + "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" + + run $PWD/hooks/command + + assert_success + assert_output --partial "ran command in docker" + + unstub docker + unset BUILDKITE_PLUGIN_DOCKER_WORKDIR + unset BUILDKITE_PLUGIN_DOCKER_IMAGE + unset BUILDKITE_PLUGIN_DOCKER_MOUNT_BUILDKITE_AGENT + unset BUILDKITE_PLUGIN_DOCKER_ENTRYPOINT + unset BUILDKITE_PLUGIN_DOCKER_SHELL + unset BUILDKITE_COMMAND +} + +@test "Runs with shell option" { + export BUILDKITE_PLUGIN_DOCKER_WORKDIR=/app + export BUILDKITE_PLUGIN_DOCKER_IMAGE=image:tag + export BUILDKITE_PLUGIN_DOCKER_MOUNT_BUILDKITE_AGENT=false + export BUILDKITE_PLUGIN_DOCKER_SHELL='custom-bash -a -b' + export BUILDKITE_COMMAND="echo hello world" + + stub docker \ + "run -it --rm --volume $PWD:/app --workdir /app image:tag custom-bash -a -b 'echo hello world' : echo ran command in docker" + + run $PWD/hooks/command + + assert_success + assert_output --partial "ran command in docker" + + unstub docker + unset BUILDKITE_PLUGIN_DOCKER_WORKDIR + unset BUILDKITE_PLUGIN_DOCKER_IMAGE + unset BUILDKITE_PLUGIN_DOCKER_MOUNT_BUILDKITE_AGENT + unset BUILDKITE_PLUGIN_DOCKER_SHELL + unset BUILDKITE_COMMAND +} + +@test "Runs with shell disabled" { + export BUILDKITE_PLUGIN_DOCKER_WORKDIR=/app + export BUILDKITE_PLUGIN_DOCKER_IMAGE=image:tag + export BUILDKITE_PLUGIN_DOCKER_MOUNT_BUILDKITE_AGENT=false + export BUILDKITE_PLUGIN_DOCKER_SHELL=false + export BUILDKITE_COMMAND="echo hello world" + + stub docker \ + "run -it --rm --volume $PWD:/app --workdir /app image:tag echo hello world : echo ran command in docker" + + run $PWD/hooks/command + + assert_success + assert_output --partial "ran command in docker" + + unstub docker + unset BUILDKITE_PLUGIN_DOCKER_WORKDIR + unset BUILDKITE_PLUGIN_DOCKER_IMAGE + unset BUILDKITE_PLUGIN_DOCKER_MOUNT_BUILDKITE_AGENT + unset BUILDKITE_PLUGIN_DOCKER_SHELL + unset BUILDKITE_COMMAND +} \ No newline at end of file