Skip to content

Commit

Permalink
Merge pull request #350 from buildkite-plugins/toote_default_tag_cach…
Browse files Browse the repository at this point in the history
…e_from_issue-349

Default tag in `cache-from`
  • Loading branch information
pzeballos authored Oct 17, 2022
2 parents 282d75f + 7ffd4e6 commit 6b2284a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
6 changes: 5 additions & 1 deletion commands/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ if [[ "$(plugin_read_config NO_CACHE "false")" == "false" ]] ; then
IFS=':' read -r -a tokens <<< "$line"
service_name=${tokens[0]}
service_image=$(IFS=':'; echo "${tokens[*]:1:2}")
service_tag=${tokens[2]}
if [ ${#tokens[@]} -gt 2 ]; then
service_tag=${tokens[2]}
else
service_tag="latest"
fi

if ! validate_tag "$service_tag"; then
echo "🚨 cache-from ${service_image} has an invalid tag so it will be ignored"
Expand Down
25 changes: 25 additions & 0 deletions tests/build.bats
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,31 @@ load '../lib/shared'
unstub docker-compose
}

@test "Build with a cache-from image with no tag" {
export BUILDKITE_BUILD_NUMBER=1
export BUILDKITE_JOB_ID=1111
export BUILDKITE_PIPELINE_SLUG=test

export BUILDKITE_PLUGIN_DOCKER_COMPOSE_BUILD_0=helloworld
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CACHE_FROM_0=helloworld:my.repository/myservice_cache
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CONFIG="tests/composefiles/docker-compose.v3.2.yml"

stub docker \
"pull my.repository/myservice_cache : 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 helloworld : echo built helloworld"

run "$PWD"/hooks/command

assert_success
assert_output --partial "pulled cache image"
assert_output --partial "- my.repository/myservice_cache"
assert_output --partial "built helloworld"
unstub docker
unstub docker-compose
}

@test "Build with several cache-from images for one service" {
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CONFIG="tests/composefiles/docker-compose.v3.2.yml"
export BUILDKITE_JOB_ID=1111
Expand Down
22 changes: 22 additions & 0 deletions tests/v2/build.bats
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,28 @@ setup_file() {
unstub docker
}

@test "Build with a cache-from image with no tag" {
export BUILDKITE_BUILD_NUMBER=1
export BUILDKITE_JOB_ID=1111
export BUILDKITE_PIPELINE_SLUG=test

export BUILDKITE_PLUGIN_DOCKER_COMPOSE_BUILD_0=helloworld
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CACHE_FROM_0=helloworld:my.repository/myservice_cache
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CONFIG="tests/composefiles/docker-compose.v3.2.yml"

stub docker \
"pull my.repository/myservice_cache : echo pulled cache image" \
"compose -f tests/composefiles/docker-compose.v3.2.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml build --pull helloworld : echo built helloworld"

run "$PWD"/hooks/command

assert_success
assert_output --partial "pulled cache image"
assert_output --partial "- my.repository/myservice_cache"
assert_output --partial "built helloworld"
unstub docker
}

@test "Build with a cache-from image with no-cache also set" {
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CONFIG="tests/composefiles/docker-compose.v3.2.yml"
export BUILDKITE_JOB_ID=1111
Expand Down

0 comments on commit 6b2284a

Please sign in to comment.