Skip to content

Commit

Permalink
Merge pull request #157 from uw-ipd/default_volumes
Browse files Browse the repository at this point in the history
Add support for default volume mounts
  • Loading branch information
toolmantim authored Jun 28, 2018
2 parents f95c956 + 2766c17 commit cdbd208
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,8 @@ Note: this option can only be specified on a `build` step.

A list of volumes to mount into the container. If a matching volume exists in the Docker Compose config file, this option will override that definition.

Additionally, volumes may be specified via the agent environment variable `BUILDKITE_DOCKER_DEFAULT_VOLUMES`, a `;` (semicolon) delimited list of mounts in the `-v` syntax. (Ex. `buildkite:/buildkite;./app:/app`).

### `leave-volumes` (optional, run only)

Prevent the removal of volumes after the command has been run.
Expand Down
7 changes: 7 additions & 0 deletions commands/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ while IFS=$'\n' read -r vol ; do
[[ -n "${vol:-}" ]] && run_params+=("-v" "$(expand_relative_volume_path "$vol")")
done <<< "$(plugin_read_list VOLUMES)"

IFS=';' read -r -a default_volumes <<< "${BUILDKITE_DOCKER_DEFAULT_VOLUMES:-}"
for vol in "${default_volumes[@]:-}"
do
vol=`echo "${vol:-}"`
[[ -n "${vol}" ]] && run_params+=("-v" "$(expand_relative_volume_path "$vol")")
done

# Optionally disable allocating a TTY
if [[ "$(plugin_read_config TTY "true")" == "false" ]] ; then
run_params+=(-T)
Expand Down
7 changes: 6 additions & 1 deletion lib/shared.bash
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ function plugin_read_config() {

# Reads either a value or a list from plugin config
function plugin_read_list() {
local prefix="BUILDKITE_PLUGIN_DOCKER_COMPOSE_$1"
prefix_read_list "BUILDKITE_PLUGIN_DOCKER_COMPOSE_$1"
}

# Reads either a value or a list from the given env prefix
function prefix_read_list() {
local prefix="$1"
local parameter="${prefix}_0"

if [[ -n "${!parameter:-}" ]]; then
Expand Down
77 changes: 77 additions & 0 deletions tests/run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,83 @@ export BUILDKITE_JOB_ID=1111
unstub buildkite-agent
}

@test "Run with an external volume" {
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=pwd
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CHECK_LINKED_CONTAINERS=false
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CLEANUP=false
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_VOLUMES="buildkite:/buildkite"

stub docker-compose \
"-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml pull myservice : echo pulled myservice" \
"-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml run --name buildkite1111_myservice_build_1 -v buildkite:/buildkite myservice pwd : echo ran myservice with volumes"

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

run $PWD/hooks/command

assert_success
assert_output --partial "ran myservice with volumes"
unstub docker-compose
unstub buildkite-agent
}
@test "Run with default volumes, extra delimiters" {
# Tests introduction of extra delimiters, as would occur if
# EXPORT BUILDKITE_DOCKER_DEFAULT_VOLUMES="new:mount; ${BUILDKITE_DOCKER_DEFAULT_VOLUMES:-}"
# was used with no existing value
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=pwd
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CHECK_LINKED_CONTAINERS=false
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CLEANUP=false
export BUILDKITE_DOCKER_DEFAULT_VOLUMES="buildkite:/buildkite; ./dist:/app/dist;;"

stub docker-compose \
"-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml pull myservice : echo pulled myservice" \
"-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml run --name buildkite1111_myservice_build_1 -v buildkite:/buildkite -v $PWD/dist:/app/dist myservice pwd : echo ran myservice with volumes"

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

run $PWD/hooks/command

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

@test "Run with default volumes" {
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=pwd
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CHECK_LINKED_CONTAINERS=false
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CLEANUP=false
export BUILDKITE_DOCKER_DEFAULT_VOLUMES="buildkite:/buildkite;./dist:/app/dist"

stub docker-compose \
"-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml pull myservice : echo pulled myservice" \
"-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml run --name buildkite1111_myservice_build_1 -v buildkite:/buildkite -v $PWD/dist:/app/dist myservice pwd : echo ran myservice with volumes"

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

run $PWD/hooks/command

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

@test "Run with multiple config files" {
export BUILDKITE_JOB_ID=1111
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice
Expand Down

0 comments on commit cdbd208

Please sign in to comment.