Skip to content

Commit

Permalink
Merge pull request #376 from francoiscampbell/pipeline-labels
Browse files Browse the repository at this point in the history
Add labels to run container
  • Loading branch information
pzeballos authored Mar 9, 2023
2 parents 892994b + 9eac919 commit 14ae3ba
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 8 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions commands/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
2 changes: 2 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ configuration:
type: integer
rm:
type: boolean
run-labels:
type: boolean
separator-cache-from:
type: string
minLength: 1
Expand Down
14 changes: 8 additions & 6 deletions tests/multiple-commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/output.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
48 changes: 47 additions & 1 deletion tests/run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1498,4 +1502,46 @@ export BUILDKITE_JOB_ID=1111
assert_output --partial "ran myservice without tty"
unstub docker-compose
unstub buildkite-agent
}
}

@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
}
3 changes: 2 additions & 1 deletion tests/v2/run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -1374,4 +1375,4 @@ export BUILDKITE_JOB_ID=1111
assert_output --partial "ran myservice without tty"
unstub docker
unstub buildkite-agent
}
}

0 comments on commit 14ae3ba

Please sign in to comment.