Skip to content

Commit

Permalink
Merge pull request #160 from buildkite-plugins/fix-cache-from-for-ser…
Browse files Browse the repository at this point in the history
…vice-names-with-dashes

Fix cache-from errors with service containing hyphens
  • Loading branch information
toolmantim authored Jul 20, 2018
2 parents 8cf0b08 + 74b5559 commit 7d212d4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
13 changes: 9 additions & 4 deletions commands/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ pull_retries="$(plugin_read_config PULL_RETRIES "0")"
override_file="docker-compose.buildkite-${BUILDKITE_BUILD_NUMBER}-override.yml"
build_images=()

service_name_cache_from_var() {
local service_name="$1"
echo "cache_from__${service_name//-/_}"
}

for line in $(plugin_read_list CACHE_FROM) ; do
IFS=':' read -r -a tokens <<< "$line"
service_name=${tokens[0]}
service_image=$(IFS=':'; echo "${tokens[*]:1}")

echo "~~~ :docker: Pulling cache image for $service_name"
if retry "$pull_retries" plugin_prompt_and_run docker pull "$service_image" ; then
printf -v "cache_from__${service_name}" %s "$service_image"
printf -v "$(service_name_cache_from_var "$service_name")" "%s" "$service_image"
else
echo "!!! :docker: Pull failed. $service_image will not be used as a cache for $service_name"
fi
Expand All @@ -31,9 +36,9 @@ for service_name in $(plugin_read_list BUILD) ; do

build_images+=("$service_name" "$image_name")

cache_key="cache_from__${service_name}"
if [[ -n "${!cache_key-}" ]]; then
build_images+=("${!cache_key}")
cache_from_var="$(service_name_cache_from_var "${service_name}")"
if [[ -n "${!cache_from_var-}" ]]; then
build_images+=("${!cache_from_var}")
else
build_images+=("")
fi
Expand Down
24 changes: 24 additions & 0 deletions tests/build.bats
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,30 @@ load '../lib/shared'
unstub docker-compose
}

@test "Build with a cache-from image with hyphen" {
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CONFIG="tests/composefiles/docker-compose.v3.2.yml"
export BUILDKITE_JOB_ID=1111
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_BUILD_0=hello-world
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CACHE_FROM_0=hello-world:my.repository/my-service_cache:latest
export BUILDKITE_PIPELINE_SLUG=test
export BUILDKITE_BUILD_NUMBER=1

stub docker \
"pull my.repository/my-service_cache:latest : echo pulled cache image"

stub docker-compose \
"-f tests/composefiles/docker-compose.v3.2.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml build --pull hello-world : echo built hello-world"

run $PWD/hooks/command

assert_success
assert_output --partial "pulled cache image"
assert_output --partial "- my.repository/my-service_cache:latest"
assert_output --partial "built hello-world"
unstub docker
unstub docker-compose
}

@test "Build with a cache-from image retry on failing pull" {
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CONFIG="tests/composefiles/docker-compose.v3.2.yml"
export BUILDKITE_JOB_ID=1111
Expand Down

0 comments on commit 7d212d4

Please sign in to comment.