Skip to content

Commit

Permalink
Merge pull request #50 from buildkite-plugins/add-interpreter-option
Browse files Browse the repository at this point in the history
Add a shell option
  • Loading branch information
toolmantim authored Jun 19, 2018
2 parents a15a53f + 09ff032 commit 843c9f7
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 7 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
23 changes: 19 additions & 4 deletions hooks/command
Original file line number Diff line number Diff line change
Expand Up @@ -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[@]}"
2 changes: 2 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ configuration:
type: boolean
runtime:
type: string
shell:
type: string
required:
- image
additionalProperties: false
75 changes: 73 additions & 2 deletions tests/command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,15 @@ 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
export BUILDKITE_PLUGIN_DOCKER_ENTRYPOINT=/some/custom/entry/point
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

Expand All @@ -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
}

0 comments on commit 843c9f7

Please sign in to comment.