Skip to content

Commit a0f1ca7

Browse files
authored
Merge pull request #285 from buildkite-plugins/adding-log-driver-opts
Adding log driver opts
2 parents db8815a + 2a19461 commit a0f1ca7

File tree

4 files changed

+111
-1
lines changed

4 files changed

+111
-1
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,22 @@ steps:
115115
mount-checkout: false
116116
```
117117

118+
You can enable custom logging drivers and logging options with the use of `log-driver` and `log-opt`:
119+
120+
```yml
121+
steps:
122+
- command: "npm run start"
123+
plugins:
124+
- docker#v5.12.0:
125+
image: "node:7"
126+
log-driver: "awslogs"
127+
log-opt:
128+
- "awslogs-group=my-buildkite-logs"
129+
- "awslogs-region=us-east-1"
130+
- "awslogs-stream-prefix=buildkite"
131+
- "awslogs-create-group=true"
132+
```
133+
118134
Variable interpolation can be tricky due to the 3 layers involved (Buildkite, agent VM, and docker). For example, if you want to use [ECR Buildkite plugin](https://github.com/buildkite-plugins/ecr-buildkite-plugin), you will need to use the following syntax. Note the `$$` prefix for variables that would otherwise resolve at pipeline upload time, not runtime:
119135

120136
```yml
@@ -307,6 +323,20 @@ Whether or not to leave the container after the run, or immediately remove it wi
307323

308324
Default: `false`
309325

326+
### `log-driver` (optional, string)
327+
328+
The logging driver for the container. This allows you to configure how Docker handles logs for the container.
329+
330+
Common drivers include: `json-file`, `syslog`, `journald`, `gelf`, `fluentd`, `awslogs`, `splunk`, `etwlogs`, `gcplogs`, `logentries`, `none`.
331+
332+
:information_source: As a default, Docker uses the [json-file logging driver](https://docs.docker.com/engine/logging/drivers/json-file/)
333+
334+
See [Docker's logging documentation](https://docs.docker.com/config/containers/logging/) for complete details.
335+
336+
### `log-opt` (optional, array)
337+
338+
Options for the logging driver. These are key-value pairs that configure the behavior of the selected logging driver.
339+
310340
### `load` (optional, string)
311341

312342
Specify a file to load a docker image from. If omitted no load will be done.
@@ -315,7 +345,7 @@ Specify a file to load a docker image from. If omitted no load will be done.
315345

316346
Whether to automatically mount the current working directory which contains your checked out codebase. Mounts onto `/workdir`, unless `workdir` is set, in which case that will be used.
317347

318-
If there's a git mirror path and `mount-checkout` is enabled, the (mirror path)[https://buildkite.com/docs/pipelines/environment-variables#BUILDKITE_REPO_MIRROR] is mounted into the docker container as an added volume. Otherwise, the git mirror path will have to be explicitly added as an extra volume to mount into the container.
348+
If there's a git mirror path and `mount-checkout` is enabled, the (mirror path)[https://buildkite.com/docs/pipelines/environment-variables#BUILDKITE_REPO_MIRROR] is mounted into the docker container as an added volume. Otherwise, the git mirror path will have to be explicitly added as an extra volume to mount into the container.
319349

320350
Default: `true`
321351

commands/run.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,18 @@ if [[ -n "${BUILDKITE_PLUGIN_DOCKER_STORAGE_OPT:-}" ]] ; then
397397
args+=("--storage-opt" "${BUILDKITE_PLUGIN_DOCKER_STORAGE_OPT:-}")
398398
fi
399399

400+
# Support docker run --log-driver
401+
if [[ -n "${BUILDKITE_PLUGIN_DOCKER_LOG_DRIVER:-}" ]] ; then
402+
args+=("--log-driver" "${BUILDKITE_PLUGIN_DOCKER_LOG_DRIVER}")
403+
fi
404+
405+
# Support docker run --log-opt
406+
if plugin_read_list_into_result BUILDKITE_PLUGIN_DOCKER_LOG_OPT; then
407+
for arg in "${result[@]}"; do
408+
args+=("--log-opt" "$arg")
409+
done
410+
fi
411+
400412
shell=()
401413
shell_disabled=1
402414

plugin.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ configuration:
4747
type: boolean
4848
load:
4949
type: string
50+
log-driver:
51+
type: string
52+
log-opt:
53+
type: array
5054
memory:
5155
type: string
5256
memory-swap:

tests/command.bats

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,70 @@ EOF
13821382
unstub docker
13831383
}
13841384

1385+
@test "Runs BUILDKITE_COMMAND with log-driver" {
1386+
export BUILDKITE_PLUGIN_DOCKER_LOG_DRIVER=json-file
1387+
export BUILDKITE_COMMAND="echo hello world"
1388+
1389+
stub docker \
1390+
"run -t -i --rm --init --volume $PWD:/workdir --workdir /workdir --log-driver json-file --label com.buildkite.job-id=1-2-3-4 image:tag /bin/sh -e -c 'echo hello world' : echo ran command in docker"
1391+
1392+
run "$PWD"/hooks/command
1393+
1394+
assert_success
1395+
assert_output --partial "ran command in docker"
1396+
1397+
unstub docker
1398+
}
1399+
1400+
@test "Runs BUILDKITE_COMMAND with log-opt" {
1401+
export BUILDKITE_PLUGIN_DOCKER_LOG_OPT_0=max-size=10m
1402+
export BUILDKITE_COMMAND="echo hello world"
1403+
1404+
stub docker \
1405+
"run -t -i --rm --init --volume $PWD:/workdir --workdir /workdir --log-opt max-size=10m --label com.buildkite.job-id=1-2-3-4 image:tag /bin/sh -e -c 'echo hello world' : echo ran command in docker"
1406+
1407+
run "$PWD"/hooks/command
1408+
1409+
assert_success
1410+
assert_output --partial "ran command in docker"
1411+
1412+
unstub docker
1413+
}
1414+
1415+
@test "Runs BUILDKITE_COMMAND with multiple log-opt" {
1416+
export BUILDKITE_PLUGIN_DOCKER_LOG_OPT_0=max-size=10m
1417+
export BUILDKITE_PLUGIN_DOCKER_LOG_OPT_1=max-file=3
1418+
export BUILDKITE_PLUGIN_DOCKER_LOG_OPT_2=labels=production
1419+
export BUILDKITE_COMMAND="echo hello world"
1420+
1421+
stub docker \
1422+
"run -t -i --rm --init --volume $PWD:/workdir --workdir /workdir --log-opt max-size=10m --log-opt max-file=3 --log-opt labels=production --label com.buildkite.job-id=1-2-3-4 image:tag /bin/sh -e -c 'echo hello world' : echo ran command in docker"
1423+
1424+
run "$PWD"/hooks/command
1425+
1426+
assert_success
1427+
assert_output --partial "ran command in docker"
1428+
1429+
unstub docker
1430+
}
1431+
1432+
@test "Runs BUILDKITE_COMMAND with log-driver and log-opt" {
1433+
export BUILDKITE_PLUGIN_DOCKER_LOG_DRIVER=syslog
1434+
export BUILDKITE_PLUGIN_DOCKER_LOG_OPT_0=syslog-address=tcp://192.168.1.3:514
1435+
export BUILDKITE_PLUGIN_DOCKER_LOG_OPT_1=tag=myapp
1436+
export BUILDKITE_COMMAND="echo hello world"
1437+
1438+
stub docker \
1439+
"run -t -i --rm --init --volume $PWD:/workdir --workdir /workdir --log-driver syslog --log-opt syslog-address=tcp://192.168.1.3:514 --log-opt tag=myapp --label com.buildkite.job-id=1-2-3-4 image:tag /bin/sh -e -c 'echo hello world' : echo ran command in docker"
1440+
1441+
run "$PWD"/hooks/command
1442+
1443+
assert_success
1444+
assert_output --partial "ran command in docker"
1445+
1446+
unstub docker
1447+
}
1448+
13851449
@test "Run with BUILDKITE_COMMAND that exits with a failure" {
13861450
export BUILDKITE_COMMAND='pwd'
13871451

0 commit comments

Comments
 (0)