diff --git a/README.md b/README.md index d68f3176..508424d1 100644 --- a/README.md +++ b/README.md @@ -335,6 +335,10 @@ This option can also be configured on the agent machine using the environment va The name to use when tagging pre-built images. If multiple images are built in the build phase, you must provide an array of image names. +### `build-alias` (optional, build only) + +Other docker-compose services that should be aliased to the main service that was built. This is for when different docker-compose services share the same prebuilt image. + ### `args` (optional, build only) A list of KEY=VALUE that are passed through as build arguments when image is being built. diff --git a/commands/build.sh b/commands/build.sh index d375b580..fdd8b000 100755 --- a/commands/build.sh +++ b/commands/build.sh @@ -87,8 +87,16 @@ if [[ -n "$image_repository" ]] ; then echo "~~~ :docker: Pushing built images to $image_repository" retry "$push_retries" run_docker_compose -f "$override_file" push "${services[@]}" + # iterate over build images while [[ ${#build_images[@]} -gt 0 ]] ; do set_prebuilt_image "${build_images[0]}" "${build_images[1]}" + + # set aliases + for service_alias in $(plugin_read_list BUILD_ALIAS) ; do + set_prebuilt_image "$service_alias" "${build_images[1]}" + done + + # pop-off the last build image build_images=("${build_images[@]:3}") done fi diff --git a/plugin.yml b/plugin.yml index fd493a8e..21230dca 100644 --- a/plugin.yml +++ b/plugin.yml @@ -29,6 +29,9 @@ configuration: args: type: [ string, array ] minimum: 1 + build-alias: + type: [ string, array ] + minimum: 1 image-repository: type: string image-name: diff --git a/tests/build.bats b/tests/build.bats index fa0bebb9..e7b30971 100644 --- a/tests/build.bats +++ b/tests/build.bats @@ -82,6 +82,36 @@ load '../lib/shared' unstub buildkite-agent } +@test "Build with a repository and multiple build aliases" { + export BUILDKITE_JOB_ID=1111 + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_BUILD=myservice + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_IMAGE_REPOSITORY=my.repository/llamas + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_BUILD_ALIAS_0=myservice-1 + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_BUILD_ALIAS_1=myservice-2 + export BUILDKITE_PIPELINE_SLUG=test + export BUILDKITE_BUILD_NUMBER=1 + + stub docker-compose \ + "-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml build --pull myservice : echo built myservice" \ + "-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml push myservice : echo pushed myservice" \ + + stub buildkite-agent \ + "meta-data set docker-compose-plugin-built-image-tag-myservice my.repository/llamas:test-myservice-build-1 : echo set image metadata for myservice" \ + "meta-data set docker-compose-plugin-built-image-tag-myservice-1 my.repository/llamas:test-myservice-build-1 : echo set image metadata for myservice-1" \ + "meta-data set docker-compose-plugin-built-image-tag-myservice-2 my.repository/llamas:test-myservice-build-1 : echo set image metadata for myservice-2" + + run $PWD/hooks/command + + assert_success + assert_output --partial "built myservice" + assert_output --partial "pushed myservice" + assert_output --partial "set image metadata for myservice" + assert_output --partial "set image metadata for myservice-1" + assert_output --partial "set image metadata for myservice-2" + unstub docker-compose + unstub buildkite-agent +} + @test "Build with a repository and push retries" { export BUILDKITE_JOB_ID=1111 export BUILDKITE_PLUGIN_DOCKER_COMPOSE_BUILD=myservice