Skip to content

Commit

Permalink
Merge pull request #95 from buildkite-plugins/fix-command-quoting-bug
Browse files Browse the repository at this point in the history
Fix command quoting bug
  • Loading branch information
lox committed Feb 8, 2018
2 parents 402ec18 + c36c50d commit 032a6e0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 30 deletions.
3 changes: 1 addition & 2 deletions .buildkite/pipeline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ steps:
command: .buildkite/steps/shellcheck
- label: run bats tests
command: tests/
plugins:
${BUILDKITE_REPO}#${commit}:
run: tests
Expand Down Expand Up @@ -114,4 +113,4 @@ steps:
push: helloworld
config: tests/composefiles/docker-compose.v2.1.yml
YAML
YAML
14 changes: 0 additions & 14 deletions Dockerfile

This file was deleted.

4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: '2'
services:
tests:
build: .
image: buildkite/plugin-tester
volumes:
- .:/app
- ".:/plugin"
22 changes: 10 additions & 12 deletions hooks/commands/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,26 @@ done <<< "$(printf '%s\n%s' \

run_params+=("$service_name")

# append command tokens if there are any. We do this to avoid word splitting
# issues as discussed in https://github.com/koalaman/shellcheck/wiki/SC2207
if [[ -n "${BUILDKITE_COMMAND:-}" ]] ; then
while IFS=$' \t\n' read -r -a line ; do
for token in "${line[@]}" ; do
run_params+=("$token")
done
done <<< "$BUILDKITE_COMMAND"
fi

(
IFS=
set +e
set -f

# The eval statements below are used to allow $BUILDKITE_COMMAND to be interpolated correctly
# When paired with -f we ensure that it word splits correctly, e.g bash -c "pwd" should split
# into [bash, -c, "pwd"]. Eval ends up the simplest way to do this, and when paired with the
# set -f above we ensure globs aren't expanded (imagine a command like `cat *`, which bash would
# helpfully expand prior to passing it to docker-compose)

if [[ -f "$override_file" ]]; then
echo "+++ :docker: Running command in Docker Compose service: $service_name" >&2
run_docker_compose "${run_params[@]}"
eval "run_docker_compose \${run_params[@]} $BUILDKITE_COMMAND"
else
echo "~~~ :docker: Building Docker Compose Service: $service_name" >&2
run_docker_compose build --pull "$service_name"

echo "+++ :docker: Running command in Docker Compose service: $service_name" >&2
run_docker_compose "${run_params[@]}"
eval "run_docker_compose \${run_params[@]} $BUILDKITE_COMMAND"
fi
)

Expand Down
25 changes: 25 additions & 0 deletions tests/run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,31 @@ load '../lib/run'
unstub buildkite-agent
}

@test "Run without a prebuilt image with a complicated command" {
export BUILDKITE_JOB_ID=1111
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice
export BUILDKITE_PIPELINE_SLUG=test
export BUILDKITE_BUILD_NUMBER=1
export BUILDKITE_COMMAND="sh -c 'echo hello world'"
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CHECK_LINKED_CONTAINERS=false
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CLEANUP=false

stub docker-compose \
"-f docker-compose.yml -p buildkite1111 build --pull myservice : echo built myservice" \
"-f docker-compose.yml -p buildkite1111 run --name buildkite1111_myservice_build_1 myservice $BUILDKITE_COMMAND : echo ran myservice"

stub buildkite-agent \
"meta-data get docker-compose-plugin-built-image-tag-myservice : exit 1"

run $PWD/hooks/command

assert_success
assert_output --partial "built myservice"
assert_output --partial "ran myservice"
unstub docker-compose
unstub buildkite-agent
}

@test "Run without a prebuilt image with custom env" {
export BUILDKITE_JOB_ID=1111
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice
Expand Down

0 comments on commit 032a6e0

Please sign in to comment.