From aa41ab7ff585eb0dd863017a519ca48fbc8b7476 Mon Sep 17 00:00:00 2001 From: Francois Campbell Date: Mon, 27 Feb 2023 13:17:19 -0500 Subject: [PATCH 1/7] Add labels to run container --- README.md | 13 +++++++++++++ commands/run.sh | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/README.md b/README.md index dde8136c..4ba3d62b 100644 --- a/README.md +++ b/README.md @@ -169,6 +169,19 @@ steps: propagate-environment: true ``` +## Container Labels + +When running a command, the plugin will automatically use add the following Docker labels to the container specified in the `run` option: +- `com.buildkite.pipeline_name=${BUILDKITE_PIPELINE_NAME}` +- `com.buildkite.pipeline_slug=${BUILDKITE_PIPELINE_SLUG}` +- `com.buildkite.build_number=${BUILDKITE_BUILD_NUMBER}` +- `com.buildkite.job_id=${BUILDKITE_JOB_ID}` +- `com.buildkite.job_label=${BUILDKITE_LABEL}` +- `com.buildkite.step_key=${BUILDKITE_STEP_KEY}` +- `com.buildkite.agent_name=${BUILDKITE_AGENT_NAME}` +- `com.buildkite.agent_id=${BUILDKITE_AGENT_ID}` + + ## Build Arguments You can use the [build args key in docker-compose.yml](https://docs.docker.com/compose/compose-file/#args) to set specific build arguments when building an image. diff --git a/commands/run.sh b/commands/run.sh index 005899eb..c58c7454 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -78,6 +78,18 @@ fi # We set a predictable container name so we can find it and inspect it later on run_params+=("run" "--name" "$container_name") +# Add useful labels to run container +run_params+=( + "--label" "com.buildkite.pipeline_name=${BUILDKITE_PIPELINE_NAME}" + "--label" "com.buildkite.pipeline_slug=${BUILDKITE_PIPELINE_SLUG}" + "--label" "com.buildkite.build_number=${BUILDKITE_BUILD_NUMBER}" + "--label" "com.buildkite.job_id=${BUILDKITE_JOB_ID}" + "--label" "com.buildkite.job_label=${BUILDKITE_LABEL}" + "--label" "com.buildkite.step_key=${BUILDKITE_STEP_KEY}" + "--label" "com.buildkite.agent_name=${BUILDKITE_AGENT_NAME}" + "--label" "com.buildkite.agent_id=${BUILDKITE_AGENT_ID}" +) + # append env vars provided in ENV or ENVIRONMENT, these are newline delimited while IFS=$'\n' read -r env ; do [[ -n "${env:-}" ]] && run_params+=("-e" "${env}") From 547d80981f3ad35b071113d4367ecdad6424ddfc Mon Sep 17 00:00:00 2001 From: Francois Campbell Date: Mon, 27 Feb 2023 13:21:00 -0500 Subject: [PATCH 2/7] add example --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 4ba3d62b..005cc0cc 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,11 @@ When running a command, the plugin will automatically use add the following Dock - `com.buildkite.agent_name=${BUILDKITE_AGENT_NAME}` - `com.buildkite.agent_id=${BUILDKITE_AGENT_ID}` +These labels can make it easier to query containers on hosts using `docker ps` for example: + +```bash +docker ps --filter "label=com.buildkite.job_label=Run tests" +``` ## Build Arguments From 335479c9abff0e37b737a03b5eb90f62f93bbb07 Mon Sep 17 00:00:00 2001 From: Francois Campbell Date: Mon, 27 Feb 2023 13:37:48 -0500 Subject: [PATCH 3/7] fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 005cc0cc..494741a9 100644 --- a/README.md +++ b/README.md @@ -171,7 +171,7 @@ steps: ## Container Labels -When running a command, the plugin will automatically use add the following Docker labels to the container specified in the `run` option: +When running a command, the plugin will automatically add the following Docker labels to the container specified in the `run` option: - `com.buildkite.pipeline_name=${BUILDKITE_PIPELINE_NAME}` - `com.buildkite.pipeline_slug=${BUILDKITE_PIPELINE_SLUG}` - `com.buildkite.build_number=${BUILDKITE_BUILD_NUMBER}` From b407ce048b04e3ed16ddf0776daa5f1905a631b1 Mon Sep 17 00:00:00 2001 From: Francois Campbell Date: Fri, 3 Mar 2023 14:16:46 -0500 Subject: [PATCH 4/7] make it a flag --- commands/run.sh | 24 +++++++++++++----------- plugin.yml | 2 ++ tests/multiple-commands.bats | 14 ++++++++------ tests/output.bats | 3 +++ tests/push.bats | 4 ++++ tests/run.bats | 6 +++++- tests/v2/run.bats | 3 ++- 7 files changed, 37 insertions(+), 19 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index c58c7454..51a49924 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -78,17 +78,19 @@ fi # We set a predictable container name so we can find it and inspect it later on run_params+=("run" "--name" "$container_name") -# Add useful labels to run container -run_params+=( - "--label" "com.buildkite.pipeline_name=${BUILDKITE_PIPELINE_NAME}" - "--label" "com.buildkite.pipeline_slug=${BUILDKITE_PIPELINE_SLUG}" - "--label" "com.buildkite.build_number=${BUILDKITE_BUILD_NUMBER}" - "--label" "com.buildkite.job_id=${BUILDKITE_JOB_ID}" - "--label" "com.buildkite.job_label=${BUILDKITE_LABEL}" - "--label" "com.buildkite.step_key=${BUILDKITE_STEP_KEY}" - "--label" "com.buildkite.agent_name=${BUILDKITE_AGENT_NAME}" - "--label" "com.buildkite.agent_id=${BUILDKITE_AGENT_ID}" -) +if [[ "$(plugin_read_config RUN_LABELS "true")" =~ ^(true|on|1)$ ]]; then + # Add useful labels to run container + run_params+=( + "--label" "com.buildkite.pipeline_name=${BUILDKITE_PIPELINE_NAME}" + "--label" "com.buildkite.pipeline_slug=${BUILDKITE_PIPELINE_SLUG}" + "--label" "com.buildkite.build_number=${BUILDKITE_BUILD_NUMBER}" + "--label" "com.buildkite.job_id=${BUILDKITE_JOB_ID}" + "--label" "com.buildkite.job_label=${BUILDKITE_LABEL}" + "--label" "com.buildkite.step_key=${BUILDKITE_STEP_KEY}" + "--label" "com.buildkite.agent_name=${BUILDKITE_AGENT_NAME}" + "--label" "com.buildkite.agent_id=${BUILDKITE_AGENT_ID}" + ) +fi # append env vars provided in ENV or ENVIRONMENT, these are newline delimited while IFS=$'\n' read -r env ; do diff --git a/plugin.yml b/plugin.yml index fd36d20c..9cf6dab7 100644 --- a/plugin.yml +++ b/plugin.yml @@ -81,6 +81,8 @@ configuration: type: integer rm: type: boolean + run-labels: + type: boolean separator-cache-from: type: string minLength: 1 diff --git a/tests/multiple-commands.bats b/tests/multiple-commands.bats index 39aa0736..7e2a71fa 100644 --- a/tests/multiple-commands.bats +++ b/tests/multiple-commands.bats @@ -8,12 +8,14 @@ load '../lib/metadata' # export BUILDKITE_AGENT_STUB_DEBUG=/dev/tty # export BATS_MOCK_TMPDIR=$PWD -# General pipeline variables -export BUILDKITE_BUILD_NUMBER=1 -export BUILDKITE_COMMAND="pwd" -export BUILDKITE_JOB_ID=12 -export BUILDKITE_PIPELINE_SLUG=test - +setup_file() { + # General pipeline variables + export BUILDKITE_BUILD_NUMBER=1 + export BUILDKITE_COMMAND="pwd" + export BUILDKITE_JOB_ID=12 + export BUILDKITE_PIPELINE_SLUG=test + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN_LABELS="false" +} @test "Build and run" { export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice diff --git a/tests/output.bats b/tests/output.bats index a7952cb7..f2845553 100644 --- a/tests/output.bats +++ b/tests/output.bats @@ -9,6 +9,9 @@ load '../lib/run' # export DOCKER_STUB_DEBUG=/dev/tty # export BATS_MOCK_TMPDIR=$PWD +setup_file() { + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN_LABELS="false" +} @test "Logs: Detect some containers KO" { export BUILDKITE_AGENT_ACCESS_TOKEN="123123" diff --git a/tests/push.bats b/tests/push.bats index cefe7996..0d71f238 100644 --- a/tests/push.bats +++ b/tests/push.bats @@ -8,6 +8,10 @@ load '../lib/shared' # export BUILDKITE_AGENT_STUB_DEBUG=/dev/tty # export BATS_MOCK_TMPDIR=$PWD +setup_file() { + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN_LABELS="false" +} + @test "Push a single service with an image in it's config" { export BUILDKITE_JOB_ID=1111 export BUILDKITE_PLUGIN_DOCKER_COMPOSE_PUSH=app diff --git a/tests/run.bats b/tests/run.bats index 05ba0845..61bd6784 100644 --- a/tests/run.bats +++ b/tests/run.bats @@ -8,6 +8,10 @@ load '../lib/run' # export BUILDKITE_AGENT_STUB_DEBUG=/dev/tty # export BATS_MOCK_TMPDIR=$PWD +setup_file() { + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN_LABELS="false" +} + @test "Run without a prebuilt image" { export BUILDKITE_JOB_ID=1111 export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice @@ -1498,4 +1502,4 @@ export BUILDKITE_JOB_ID=1111 assert_output --partial "ran myservice without tty" unstub docker-compose unstub buildkite-agent -} \ No newline at end of file +} diff --git a/tests/v2/run.bats b/tests/v2/run.bats index f859dc88..3856736e 100644 --- a/tests/v2/run.bats +++ b/tests/v2/run.bats @@ -10,6 +10,7 @@ load '../../lib/run' setup_file() { export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CLI_VERSION=2 + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN_LABELS="false" } @test "Run without a prebuilt image" { @@ -1374,4 +1375,4 @@ export BUILDKITE_JOB_ID=1111 assert_output --partial "ran myservice without tty" unstub docker unstub buildkite-agent -} \ No newline at end of file +} From 9d628ebd199adfd0cc958f7ee8889d62f991c2ad Mon Sep 17 00:00:00 2001 From: Francois Campbell Date: Fri, 3 Mar 2023 15:42:21 -0500 Subject: [PATCH 5/7] Add test --- tests/run.bats | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/run.bats b/tests/run.bats index 61bd6784..0a1bf3f5 100644 --- a/tests/run.bats +++ b/tests/run.bats @@ -1503,3 +1503,42 @@ export BUILDKITE_JOB_ID=1111 unstub docker-compose unstub buildkite-agent } + +@test "Run with Docker labels" { + export BUILDKITE_JOB_ID=1111 + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice + export BUILDKITE_PIPELINE_SLUG=test + export BUILDKITE_BUILD_NUMBER=1 + export BUILDKITE_COMMAND=pwd + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN_LABELS="true" + export BUILDKITE_PIPELINE_NAME="label-test" + export BUILDKITE_LABEL="Testjob" + export BUILDKITE_STEP_KEY="test-job" + export BUILDKITE_AGENT_NAME="agent" + export BUILDKITE_AGENT_ID="1234" + + stub docker-compose \ + "-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml pull myservice : echo pulled myservice" \ + "-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml up -d --scale myservice=0 myservice : echo started dependencies for myservice" \ + "-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml run --name buildkite1111_myservice_build_1 \ + --label com.buildkite.pipeline_name=${BUILDKITE_PIPELINE_NAME} \ + --label com.buildkite.pipeline_slug=${BUILDKITE_PIPELINE_SLUG} \ + --label com.buildkite.build_number=${BUILDKITE_BUILD_NUMBER} \ + --label com.buildkite.job_id=${BUILDKITE_JOB_ID} \ + --label com.buildkite.job_label=${BUILDKITE_LABEL} \ + --label com.buildkite.step_key=${BUILDKITE_STEP_KEY} \ + --label com.buildkite.agent_name=${BUILDKITE_AGENT_NAME} \ + --label com.buildkite.agent_id=${BUILDKITE_AGENT_ID} \ + --rm myservice /bin/sh -e -c $'pwd' : echo ran myservice" + + stub buildkite-agent \ + "meta-data exists docker-compose-plugin-built-image-tag-myservice : echo myimage" \ + "meta-data get docker-compose-plugin-built-image-tag-myservice : echo myimage" + + run "$PWD"/hooks/command + + assert_success + assert_output --partial "ran myservice" + unstub docker-compose + unstub buildkite-agent +} From f7909e603ad47e046a76e1a2e7523b054d735480 Mon Sep 17 00:00:00 2001 From: Francois Campbell Date: Fri, 3 Mar 2023 15:49:25 -0500 Subject: [PATCH 6/7] update readme to add flag --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 494741a9..61544fcd 100644 --- a/README.md +++ b/README.md @@ -187,6 +187,8 @@ These labels can make it easier to query containers on hosts using `docker ps` f docker ps --filter "label=com.buildkite.job_label=Run tests" ``` +This behaviour can be disabled with the `run-labels: false` option. + ## Build Arguments You can use the [build args key in docker-compose.yml](https://docs.docker.com/compose/compose-file/#args) to set specific build arguments when building an image. @@ -636,6 +638,12 @@ If set to true, docker compose will remove the primary container after run. Equi The default is `true`. +### `run-labels` (optional, run only) + +If set to true, adds useful Docker labels to the primary container. See [Container Labels](#container-labels) for more info. + +The default is `true`. + ### `compatibility` (optional, run only) If set to true, all docker compose commands will rum with compatibility mode. Equivalent to `--compatibility` in docker-compose. From 9eac919abb44d5363b0f4be58ce6fb4a577aceaa Mon Sep 17 00:00:00 2001 From: Francois Campbell Date: Mon, 6 Mar 2023 14:22:58 -0500 Subject: [PATCH 7/7] remove unnecessary setup_file, order env vars in test --- tests/push.bats | 4 ---- tests/run.bats | 17 ++++++++++------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/tests/push.bats b/tests/push.bats index 0d71f238..cefe7996 100644 --- a/tests/push.bats +++ b/tests/push.bats @@ -8,10 +8,6 @@ load '../lib/shared' # export BUILDKITE_AGENT_STUB_DEBUG=/dev/tty # export BATS_MOCK_TMPDIR=$PWD -setup_file() { - export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN_LABELS="false" -} - @test "Push a single service with an image in it's config" { export BUILDKITE_JOB_ID=1111 export BUILDKITE_PLUGIN_DOCKER_COMPOSE_PUSH=app diff --git a/tests/run.bats b/tests/run.bats index 0a1bf3f5..b0754d2b 100644 --- a/tests/run.bats +++ b/tests/run.bats @@ -1505,17 +1505,20 @@ export BUILDKITE_JOB_ID=1111 } @test "Run with Docker labels" { - export BUILDKITE_JOB_ID=1111 - export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice - export BUILDKITE_PIPELINE_SLUG=test + # Pipeline vars + export BUILDKITE_AGENT_ID="1234" + export BUILDKITE_AGENT_NAME="agent" export BUILDKITE_BUILD_NUMBER=1 export BUILDKITE_COMMAND=pwd - export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN_LABELS="true" - export BUILDKITE_PIPELINE_NAME="label-test" + export BUILDKITE_JOB_ID=1111 export BUILDKITE_LABEL="Testjob" + export BUILDKITE_PIPELINE_NAME="label-test" + export BUILDKITE_PIPELINE_SLUG=test export BUILDKITE_STEP_KEY="test-job" - export BUILDKITE_AGENT_NAME="agent" - export BUILDKITE_AGENT_ID="1234" + + # Plugin config + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN_LABELS="true" stub docker-compose \ "-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml pull myservice : echo pulled myservice" \