Skip to content

Commit 8151c82

Browse files
authored
Merge pull request #255 from francoiscampbell/run-labels
Add Docker labels to container
2 parents b7fc590 + 78251cb commit 8151c82

File tree

5 files changed

+83
-4
lines changed

5 files changed

+83
-4
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,12 @@ Specify the IPC mode to use. See the [docker run options documentation](https://
293293

294294
Example: `host`
295295

296+
### `run-labels` (optional, boolean)
297+
298+
If set to true, adds useful Docker labels to the container. See [Container Labels](#container-labels) for more info.
299+
300+
The default is `true`.
301+
296302
### `shell` (optional, array or boolean)
297303

298304
Set the shell to use for the command. Set it to `false` to pass the command directly to the `docker run` command. The default is `["/bin/sh", "-e", "-c"]` unless you have provided an `entrypoint` or `command`.
@@ -426,6 +432,26 @@ can be found in https://docs.docker.com/config/containers/resource_constraints/#
426432

427433
Example: `0`
428434

435+
## Container Labels
436+
437+
When running a command, the plugin will automatically add the following Docker labels to the container:
438+
- `com.buildkite.pipeline_name=${BUILDKITE_PIPELINE_NAME}`
439+
- `com.buildkite.pipeline_slug=${BUILDKITE_PIPELINE_SLUG}`
440+
- `com.buildkite.build_number=${BUILDKITE_BUILD_NUMBER}`
441+
- `com.buildkite.job_id=${BUILDKITE_JOB_ID}`
442+
- `com.buildkite.job_label=${BUILDKITE_LABEL}`
443+
- `com.buildkite.step_key=${BUILDKITE_STEP_KEY}`
444+
- `com.buildkite.agent_name=${BUILDKITE_AGENT_NAME}`
445+
- `com.buildkite.agent_id=${BUILDKITE_AGENT_ID}`
446+
447+
These labels can make it easier to query containers on hosts using `docker ps` for example:
448+
449+
```bash
450+
docker ps --filter "label=com.buildkite.job_label=Run tests"
451+
```
452+
453+
This behaviour can be disabled with the `run-labels: false` option.
454+
429455
## Developing
430456

431457
To run testing, shellchecks and plugin linting use use `bk run` with the [Buildkite CLI](https://github.com/buildkite/cli).

hooks/command

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,21 @@ elif plugin_read_list_into_result BUILDKITE_PLUGIN_DOCKER_SHELL ; then
503503
fi
504504

505505
# Add the job id as meta-data for reference in pre-exit
506-
args+=("--label" "com.buildkite.job-id=${BUILDKITE_JOB_ID}")
506+
args+=("--label" "com.buildkite.job-id=${BUILDKITE_JOB_ID}") # Keep the kebab-case one for backwards compat
507+
508+
# Add useful labels to run container
509+
if [[ "${BUILDKITE_PLUGIN_DOCKER_RUN_LABELS:-true}" =~ ^(true|on|1)$ ]] ; then
510+
args+=(
511+
"--label" "com.buildkite.pipeline_name=${BUILDKITE_PIPELINE_NAME}"
512+
"--label" "com.buildkite.pipeline_slug=${BUILDKITE_PIPELINE_SLUG}"
513+
"--label" "com.buildkite.build_number=${BUILDKITE_BUILD_NUMBER}"
514+
"--label" "com.buildkite.job_id=${BUILDKITE_JOB_ID}"
515+
"--label" "com.buildkite.job_label=${BUILDKITE_LABEL}"
516+
"--label" "com.buildkite.step_key=${BUILDKITE_STEP_KEY}"
517+
"--label" "com.buildkite.agent_name=${BUILDKITE_AGENT_NAME}"
518+
"--label" "com.buildkite.agent_id=${BUILDKITE_AGENT_ID}"
519+
)
520+
fi
507521

508522
# Add the image in before the shell and command
509523
args+=("${image}")

plugin.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ configuration:
5353
type: string
5454
runtime:
5555
type: string
56+
run-labels:
57+
type: boolean
5658
shell:
5759
type: [boolean, array]
5860
shm-size:

tests/command.bats

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ setup() {
1111
export BUILDKITE_PLUGIN_DOCKER_CLEANUP=false
1212
export BUILDKITE_PLUGIN_DOCKER_MOUNT_BUILDKITE_AGENT=false
1313
export BUILDKITE_COMMAND="pwd"
14+
export BUILDKITE_PLUGIN_DOCKER_RUN_LABELS="false"
1415
}
1516

1617
@test "Run with BUILDKITE_COMMAND" {
@@ -723,6 +724,41 @@ EOF
723724
unstub docker
724725
}
725726

727+
@test "Runs BUILDKITE_COMMAND with run-labels" {
728+
export BUILDKITE_PLUGIN_DOCKER_RUN_LABELS="true"
729+
export BUILDKITE_COMMAND="echo hello world"
730+
731+
# Pipeline vars
732+
export BUILDKITE_AGENT_ID="1234"
733+
export BUILDKITE_AGENT_NAME="agent"
734+
export BUILDKITE_BUILD_NUMBER=1
735+
export BUILDKITE_JOB_ID=1111
736+
export BUILDKITE_LABEL="Testjob"
737+
export BUILDKITE_PIPELINE_NAME="label-test"
738+
export BUILDKITE_PIPELINE_SLUG=test
739+
export BUILDKITE_STEP_KEY="test-job"
740+
741+
stub docker \
742+
"run -t -i --rm --init --volume $PWD:/workdir --workdir /workdir \
743+
--label com.buildkite.job-id=${BUILDKITE_JOB_ID} \
744+
--label com.buildkite.pipeline_name=${BUILDKITE_PIPELINE_NAME} \
745+
--label com.buildkite.pipeline_slug=${BUILDKITE_PIPELINE_SLUG} \
746+
--label com.buildkite.build_number=${BUILDKITE_BUILD_NUMBER} \
747+
--label com.buildkite.job_id=${BUILDKITE_JOB_ID} \
748+
--label com.buildkite.job_label=${BUILDKITE_LABEL} \
749+
--label com.buildkite.step_key=${BUILDKITE_STEP_KEY} \
750+
--label com.buildkite.agent_name=${BUILDKITE_AGENT_NAME} \
751+
--label com.buildkite.agent_id=${BUILDKITE_AGENT_ID} \
752+
image:tag /bin/sh -e -c 'echo hello world' : echo ran command in docker"
753+
754+
run $PWD/hooks/command
755+
756+
assert_success
757+
assert_output --partial "ran command in docker"
758+
759+
unstub docker
760+
}
761+
726762
@test "Runs with a command as a string" {
727763
export BUILDKITE_PLUGIN_DOCKER_COMMAND="echo hello world"
728764
export BUILDKITE_COMMAND=
@@ -1241,7 +1277,7 @@ EOF
12411277
@test "Use ssh agent (true)" {
12421278
skip 'Can not create a socket for testing :('
12431279
export BUILDKITE_PLUGIN_DOCKER_MOUNT_SSH_AGENT=true
1244-
export SSH_AUTH_SOCK="/tmp/sock"
1280+
export SSH_AUTH_SOCK="/tmp/sock"
12451281
touch /tmp/sock # does not work as the hook checks that this is a socket
12461282

12471283
stub docker \
@@ -1258,7 +1294,7 @@ EOF
12581294
@test "Use ssh agent (with path)" {
12591295
skip 'Can not create a socket for testing :('
12601296
export BUILDKITE_PLUGIN_DOCKER_MOUNT_SSH_AGENT=/test/path
1261-
export SSH_AUTH_SOCK="/tmp/sock"
1297+
export SSH_AUTH_SOCK="/tmp/sock"
12621298
touch /tmp/sock # does not work as the hook checks that this is a socket
12631299

12641300
stub docker \

tests/windows.bats

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ setup() {
99
export BUILDKITE_PLUGIN_DOCKER_MOUNT_BUILDKITE_AGENT=false
1010
export BUILDKITE_COMMAND="pwd"
1111
export OSTYPE="win" # important to define these test as windows
12+
export BUILDKITE_PLUGIN_DOCKER_RUN_LABELS="false"
1213
}
1314

1415
@test "Run with BUILDKITE_COMMAND" {
@@ -29,4 +30,4 @@ setup() {
2930

3031
unstub docker
3132
unstub cmd.exe
32-
}
33+
}

0 commit comments

Comments
 (0)