diff --git a/README.md b/README.md index f1e52336..5bdd7843 100644 --- a/README.md +++ b/README.md @@ -464,7 +464,7 @@ Whether to skip the repository checkout phase. This is useful for steps that use ### `skip-pull` (optional, run only) -Completely avoid running any `pull` command. Images being used will need to be present in the machine from before or have been built in the same step. Could be useful to avoid hitting rate limits when you can be sure the operation is unnecessary. Note that it is possible other commands run in the plugin's lifecycle will trigger a pull of necessary images. +Completely avoid running any `pull` command. Images being used will need to be present in the machine from before or have been built in the same step. Could be useful to avoid hitting rate limits when you can be sure the operation is unnecessary. Note that it is possible other commands run in the plugin's lifecycle will trigger a pull of necessary images. ### `workdir` (optional, run only) @@ -508,6 +508,12 @@ This option can also be configured on the agent machine using the environment va A list of images to pull caches from in the format `service:index.docker.io/myorg/myrepo/myapp:tag` before building, ignoring any failures. If multiple images are listed for a service, the first one to successfully pull will be used. Requires docker-compose file version `3.2+`. +### `target` (optional, build only) + +Allow for intermediate builds with `--target VALUE` options. + +Note that there is a single build command run for all services so the target value will apply to all of them. + ### `volumes` (optional, run only) 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. diff --git a/commands/build.sh b/commands/build.sh index 7a687be9..b382a771 100755 --- a/commands/build.sh +++ b/commands/build.sh @@ -147,6 +147,11 @@ if [[ "$(plugin_read_config BUILD_PARALLEL "false")" == "true" ]] ; then build_params+=(--parallel) fi +target="$(plugin_read_config TARGET "")" +if [[ -n "$target" ]] ; then + build_params+=(--target "$target") +fi + while read -r arg ; do [[ -n "${arg:-}" ]] && build_params+=("--build-arg" "${arg}") done <<< "$(plugin_read_list ARGS)" diff --git a/plugin.yml b/plugin.yml index 05287636..872c4a88 100644 --- a/plugin.yml +++ b/plugin.yml @@ -14,81 +14,83 @@ configuration: push: type: [ string, array ] minimum: 1 - pull: + ansi: + type: boolean + args: type: [ string, array ] minimum: 1 - config: + build-alias: type: [ string, array ] minimum: 1 - env: + cache-from: type: [ string, array ] minimum: 1 - environment: + cli-version: + type: string + enum: + - 1 + - 2 + command: + type: array + config: type: [ string, array ] minimum: 1 - args: + dependencies: + type: boolean + entrypoint: + type: string + env: type: [ string, array ] minimum: 1 - build-alias: + environment: type: [ string, array ] minimum: 1 + expand-volume-vars: + type: boolean image-repository: type: string image-name: type: [ string, array ] - pull-retries: - type: integer - push-retries: - type: integer - cache-from: - type: [ string, array ] - minimum: 1 - volumes: - type: [ string, array ] - minimum: 1 - expand-volume-vars: + leave-volumes: type: boolean - command: - type: array - skip-checkout: + mount-buildkite-agent: type: boolean - leave-volumes: + mount-ssh-agent: type: boolean no-cache: type: boolean - use-aliases: + propagate-environment: type: boolean - tty: + propagate-uid-gid: type: boolean - dependencies: + pull: + type: [ string, array ] + minimum: 1 + pull-retries: + type: integer + push-retries: + type: integer + rm: type: boolean - ansi: + skip-checkout: type: boolean - verbose: + skip-pull: type: boolean - workdir: + target: type: string - rm: - type: boolean - skip-pull: + tty: type: boolean upload-container-logs: type: string - propagate-uid-gid: - type: boolean - propagate-environment: - type: boolean - mount-ssh-agent: + use-aliases: type: boolean - mount-buildkite-agent: + verbose: type: boolean - entrypoint: - type: string - cli-version: + volumes: + type: [ string, array ] + minimum: 1 + workdir: type: string - enum: - - 1 - - 2 anyOf: - required: - run @@ -112,6 +114,7 @@ configuration: pull: [ run ] push-retries: [ push ] skip-pull: [ run ] + target: [ build ] tty: [ run ] use-aliases: [ run ] user: [ run ] diff --git a/tests/build.bats b/tests/build.bats index be40072f..30efdf35 100644 --- a/tests/build.bats +++ b/tests/build.bats @@ -557,7 +557,7 @@ load '../lib/shared' stub docker-compose \ "-f tests/composefiles/docker-compose.v3.2.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml build --pull \* : echo built \$9" - run $PWD/hooks/command + run "$PWD"/hooks/command assert_success assert_output --partial "pulled cache image" @@ -649,7 +649,7 @@ load '../lib/shared' @test "Build with an invalid image-name (too long) " { export BUILDKITE_JOB_ID=1111 export BUILDKITE_PLUGIN_DOCKER_COMPOSE_BUILD=myservice - # numbers from 1 to 69 result in 129 characters + # shellcheck disable=SC2155 # numbers from 1 to 69 result in 129 characters export BUILDKITE_PLUGIN_DOCKER_COMPOSE_IMAGE_NAME="$(seq 69 | tr -d "\n")" export BUILDKITE_PLUGIN_DOCKER_COMPOSE_IMAGE_REPOSITORY=my.repository/llamas export BUILDKITE_BUILD_NUMBER=1 @@ -714,3 +714,23 @@ load '../lib/shared' unstub docker-compose unstub buildkite-agent } + +@test "Build with target" { + export BUILDKITE_BUILD_NUMBER=1 + export BUILDKITE_JOB_ID=1111 + export BUILDKITE_PIPELINE_SLUG=test + + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_BUILD=myservice + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_TARGET=intermediate + + stub docker-compose \ + "-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml build --pull --target \* \* : echo built \${11} with target \${10}" + + run "$PWD"/hooks/command + + assert_success + assert_output --partial "built myservice" + assert_output --partial "with target intermediate" + + unstub docker-compose +} \ No newline at end of file diff --git a/tests/v2/build.bats b/tests/v2/build.bats index e5733b7e..a61bed27 100644 --- a/tests/v2/build.bats +++ b/tests/v2/build.bats @@ -20,7 +20,7 @@ setup_file() { stub docker \ "compose -f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml build --pull myservice : echo built myservice" - run $PWD/hooks/command + run "$PWD"/hooks/command unstub docker assert_success @@ -37,7 +37,7 @@ setup_file() { stub docker \ "compose -f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml build --pull --no-cache myservice : echo built myservice" - run $PWD/hooks/command + run "$PWD"/hooks/command assert_success assert_output --partial "built myservice" @@ -54,7 +54,7 @@ setup_file() { stub docker \ "compose -f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml build --pull --parallel myservice : echo built myservice" - run $PWD/hooks/command + run "$PWD"/hooks/command assert_success assert_output --partial "built myservice" @@ -72,7 +72,7 @@ setup_file() { stub docker \ "compose -f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml build --pull --build-arg MYARG=0 --build-arg MYARG=1 myservice : echo built myservice" - run $PWD/hooks/command + run "$PWD"/hooks/command assert_success assert_output --partial "built myservice" @@ -93,7 +93,7 @@ setup_file() { 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" - run $PWD/hooks/command + run "$PWD"/hooks/command assert_success assert_output --partial "built myservice" @@ -121,7 +121,7 @@ setup_file() { "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 + run "$PWD"/hooks/command assert_success assert_output --partial "built myservice" @@ -150,7 +150,7 @@ setup_file() { 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" - run $PWD/hooks/command + run "$PWD"/hooks/command assert_success assert_output --partial "built myservice" @@ -175,7 +175,7 @@ setup_file() { stub buildkite-agent \ "meta-data set docker-compose-plugin-built-image-tag-myservice-tests/composefiles/docker-compose.v2.0.yml my.repository/llamas:test-myservice-build-1 : echo set image metadata for myservice" - run $PWD/hooks/command + run "$PWD"/hooks/command assert_success assert_output --partial "built myservice" @@ -201,7 +201,7 @@ setup_file() { stub buildkite-agent \ "meta-data set docker-compose-plugin-built-image-tag-myservice-tests/composefiles/docker-compose.v2.0.yml-tests/composefiles/docker-compose.v2.1.yml my.repository/llamas:test-myservice-build-1 : echo set image metadata for myservice" - run $PWD/hooks/command + run "$PWD"/hooks/command assert_success assert_output --partial "built myservice" @@ -227,7 +227,7 @@ setup_file() { "meta-data set docker-compose-plugin-built-image-tag-myservice1 my.repository/llamas:test-myservice1-build-1 : echo set image metadata for myservice1" \ "meta-data set docker-compose-plugin-built-image-tag-myservice2 my.repository/llamas:test-myservice2-build-1 : echo set image metadata for myservice2" - run $PWD/hooks/command + run "$PWD"/hooks/command assert_success assert_output --partial "built all services" @@ -245,7 +245,7 @@ setup_file() { export BUILDKITE_PIPELINE_SLUG=test export BUILDKITE_BUILD_NUMBER=1 - run $PWD/hooks/command + run "$PWD"/hooks/command assert_failure assert_output --partial "Compose file versions 2.0 and above" @@ -263,7 +263,7 @@ setup_file() { "pull my.repository/myservice_cache:latest : 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 + run "$PWD"/hooks/command assert_success assert_output --partial "pulled cache image" @@ -305,7 +305,7 @@ setup_file() { stub docker \ "compose -f tests/composefiles/docker-compose.v3.2.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml build --pull --no-cache helloworld : echo built helloworld" - run $PWD/hooks/command + run "$PWD"/hooks/command assert_success refute_output --partial "pulled cache image" @@ -327,7 +327,7 @@ setup_file() { "pull my.repository/myservice_cache:branch-name : 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 + run "$PWD"/hooks/command assert_success assert_output --partial "pulled cache image" @@ -351,7 +351,7 @@ setup_file() { "pull my.repository/myservice_cache:latest : 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 + run "$PWD"/hooks/command assert_success assert_output --partial "pulled cache image" @@ -373,7 +373,7 @@ setup_file() { "pull my.repository/myservice_cache:latest : exit 1" \ "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 + run "$PWD"/hooks/command assert_success assert_output --partial "my.repository/myservice_cache:latest will not be used as a cache for helloworld" @@ -394,7 +394,7 @@ setup_file() { "pull my.repository/my-service_cache:latest : echo pulled cache image" \ "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 + run "$PWD"/hooks/command assert_success assert_output --partial "pulled cache image" @@ -415,7 +415,7 @@ setup_file() { "pull my.repository/my-service_cache:latest : echo pulled cache image" \ "compose -f tests/composefiles/docker-compose.v3.2.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml build --pull \* : echo built \${10}" - run $PWD/hooks/command + run "$PWD"/hooks/command assert_success assert_output --partial "pulled cache image" @@ -439,7 +439,7 @@ setup_file() { "pull my.repository/myservice_cache:latest : 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 + run "$PWD"/hooks/command assert_success assert_output --partial "pulled cache image" @@ -463,7 +463,7 @@ setup_file() { stub buildkite-agent \ "meta-data set docker-compose-plugin-built-image-tag-myservice my.repository/llamas:my-llamas-image : echo set image metadata for myservice" - run $PWD/hooks/command + run "$PWD"/hooks/command assert_success assert_output --partial "built myservice" @@ -502,7 +502,7 @@ setup_file() { @test "Build with an invalid image-name (too long) " { export BUILDKITE_JOB_ID=1111 export BUILDKITE_PLUGIN_DOCKER_COMPOSE_BUILD=myservice - # numbers from 1 to 69 result in 129 characters + # shellcheck disable=SC2155 # numbers from 1 to 69 result in 129 characters export BUILDKITE_PLUGIN_DOCKER_COMPOSE_IMAGE_NAME="$(seq 69 | tr -d "\n")" export BUILDKITE_PLUGIN_DOCKER_COMPOSE_IMAGE_REPOSITORY=my.repository/llamas export BUILDKITE_BUILD_NUMBER=1 @@ -529,7 +529,7 @@ setup_file() { stub buildkite-agent \ "meta-data set docker-compose-plugin-built-image-tag-myservice-tests/composefiles/docker-compose.v3.2.yml my.repository/llamas:my-llamas-image : echo set image metadata for myservice" - run $PWD/hooks/command + run "$PWD"/hooks/command assert_success assert_output --partial "built myservice" @@ -557,7 +557,7 @@ setup_file() { "meta-data set docker-compose-plugin-built-image-tag-myservice1 my.repository/llamas:my-llamas-image-1 : echo set image metadata for myservice1" \ "meta-data set docker-compose-plugin-built-image-tag-myservice2 my.repository/llamas:my-llamas-image-2 : echo set image metadata for myservice2" - run $PWD/hooks/command + run "$PWD"/hooks/command assert_success assert_output --partial "built all services" @@ -567,3 +567,23 @@ setup_file() { unstub docker unstub buildkite-agent } + +@test "Build with target" { + export BUILDKITE_BUILD_NUMBER=1 + export BUILDKITE_JOB_ID=1111 + export BUILDKITE_PIPELINE_SLUG=test + + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_BUILD=myservice + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_TARGET=intermediate + + stub docker \ + "compose -f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml build --pull --target \* \* : echo built \${12} with target \${11}" + + run "$PWD"/hooks/command + + assert_success + assert_output --partial "built myservice" + assert_output --partial "with target intermediate" + + unstub docker +} \ No newline at end of file