diff --git a/README.md b/README.md index f40e581d..91b8fce0 100644 --- a/README.md +++ b/README.md @@ -169,6 +169,26 @@ steps: propagate-environment: true ``` +## Container Labels + +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}` +- `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}` + +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" +``` + +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. @@ -620,6 +640,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. diff --git a/commands/run.sh b/commands/run.sh index 005899eb..51a49924 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -78,6 +78,20 @@ fi # We set a predictable container name so we can find it and inspect it later on run_params+=("run" "--name" "$container_name") +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 [[ -n "${env:-}" ]] && run_params+=("-e" "${env}") 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/run.bats b/tests/run.bats index 05ba0845..b0754d2b 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,46 @@ export BUILDKITE_JOB_ID=1111 assert_output --partial "ran myservice without tty" unstub docker-compose unstub buildkite-agent -} \ No newline at end of file +} + +@test "Run with Docker labels" { + # Pipeline vars + export BUILDKITE_AGENT_ID="1234" + export BUILDKITE_AGENT_NAME="agent" + export BUILDKITE_BUILD_NUMBER=1 + export BUILDKITE_COMMAND=pwd + 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" + + # 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" \ + "-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 +} 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 +}