From d9d0bb051eba3ed1116422248e41c5826672ba04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Wed, 12 Oct 2022 18:01:18 -0300 Subject: [PATCH 1/5] Sorted plugin properties alphabetically --- plugin.yml | 86 +++++++++++++++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/plugin.yml b/plugin.yml index 05287636..108bb9a4 100644 --- a/plugin.yml +++ b/plugin.yml @@ -14,81 +14,81 @@ 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: - type: boolean - command: - type: array - skip-checkout: - type: boolean leave-volumes: type: boolean - no-cache: - type: boolean - use-aliases: + mount-buildkite-agent: type: boolean - tty: + mount-ssh-agent: type: boolean - dependencies: + no-cache: type: boolean - ansi: + propagate-environment: type: boolean - verbose: + propagate-uid-gid: type: boolean - workdir: - type: string + pull: + type: [ string, array ] + minimum: 1 + pull-retries: + type: integer + push-retries: + type: integer rm: type: boolean + skip-checkout: + type: boolean skip-pull: type: boolean + 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 From c45678374da6f9916c38dfd6b8c641d42a204f18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Wed, 12 Oct 2022 18:21:09 -0300 Subject: [PATCH 2/5] Added target option --- commands/build.sh | 5 +++++ plugin.yml | 3 +++ 2 files changed, 8 insertions(+) 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 108bb9a4..872c4a88 100644 --- a/plugin.yml +++ b/plugin.yml @@ -76,6 +76,8 @@ configuration: type: boolean skip-pull: type: boolean + target: + type: string tty: type: boolean upload-container-logs: @@ -112,6 +114,7 @@ configuration: pull: [ run ] push-retries: [ push ] skip-pull: [ run ] + target: [ build ] tty: [ run ] use-aliases: [ run ] user: [ run ] From 8422b242f517c214ea00704f60a5758a9c3991bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Wed, 12 Oct 2022 18:21:55 -0300 Subject: [PATCH 3/5] Added tests on new option --- tests/build.bats | 20 ++++++++++++++++++++ tests/v2/build.bats | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/tests/build.bats b/tests/build.bats index be40072f..5b09d0c8 100644 --- a/tests/build.bats +++ b/tests/build.bats @@ -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..ba96142d 100644 --- a/tests/v2/build.bats +++ b/tests/v2/build.bats @@ -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 From 148de07a2fa4aab383f44dc0aa4b4bcdc88b8c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Wed, 12 Oct 2022 18:25:17 -0300 Subject: [PATCH 4/5] Corrected all shellcheck warnings --- tests/build.bats | 4 ++-- tests/v2/build.bats | 46 ++++++++++++++++++++++----------------------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/tests/build.bats b/tests/build.bats index 5b09d0c8..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 diff --git a/tests/v2/build.bats b/tests/v2/build.bats index ba96142d..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" From dfc2402cd55a3dc6ff502cc37c42b496dc2f25d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Wed, 12 Oct 2022 18:29:07 -0300 Subject: [PATCH 5/5] Added documentation for target option --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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.