diff --git a/commands/build.sh b/commands/build.sh index 57d1c859..640d39e4 100755 --- a/commands/build.sh +++ b/commands/build.sh @@ -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" diff --git a/tests/build.bats b/tests/build.bats index 1d3b7531..5edd4f5d 100644 --- a/tests/build.bats +++ b/tests/build.bats @@ -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 diff --git a/tests/v2/build.bats b/tests/v2/build.bats index 30389a69..e45de20a 100644 --- a/tests/v2/build.bats +++ b/tests/v2/build.bats @@ -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