Skip to content

Commit 79fd417

Browse files
committed
Add chown setting to fix root permissions on exit
1 parent d8b7c3d commit 79fd417

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ Will propagate `GOOGLE_APPLICATION_CREDENTIALS`, `CLOUDSDK_AUTH_CREDENTIAL_FILE_
287287

288288
Whether to match the user ID and group ID for the container user to the user ID and group ID for the host user. It is similar to specifying `user: 1000:1000`, except it avoids hardcoding a particular user/group ID.
289289

290-
Using this option ensures that any files created on shared mounts from within the container will be accessible to the host user. It is otherwise common to accidentally create root-owned files that Buildkite will be unable to remove, since containers by default run as the root user.
290+
Using this option ensures that any files created on shared mounts from within the container will be accessible to the host user. It is otherwise common to accidentally create root-owned files that the agent will be unable to remove, since a lot of images by default run as the root user. This can also be mitigated for the checkout directory by using the `chown` option of this plugin.
291291

292292
### `privileged` (optional, boolean)
293293

@@ -319,6 +319,20 @@ If there's a git mirror path and `mount-checkout` is enabled, the (mirror path)[
319319

320320
Default: `true`
321321

322+
### `chown` (optional, boolean)
323+
324+
Whether to `chown` the directory mounted with `mount-checkout` to the Buildkite agent user before exiting, to ensure that the agent can clean up any additional files created there. This will happen even if the command fails or is cancelled. This option has no effect on Windows or if `mount-checkout` is false.
325+
326+
Prefer using `propagate-uid-gid` over this option, as the `chown`–which can take some time if your checkout is of considerable size—is likely not needed at all in that case.
327+
328+
Default: `false`
329+
330+
### `chown-image` (optional, string)
331+
332+
The Docker image in which to run the `chown` command.
333+
334+
Default: `busybox`
335+
322336
### `mount-buildkite-agent` (optional, boolean)
323337

324338
Whether to automatically mount the `buildkite-agent` binary from the host agent machine into the container.

hooks/pre-exit

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
#!/bin/bash
22
set -euo pipefail
33

4+
DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
5+
6+
# shellcheck source=lib/shared.bash
7+
. "$DIR/../lib/shared.bash"
8+
49
if [[ "${BUILDKITE_PLUGIN_DOCKER_CLEANUP:-true}" =~ ^(true|on|1)$ ]] ; then
510
for container in $(docker ps -a -q --filter "label=com.buildkite.job-id=${BUILDKITE_JOB_ID}") ; do
611
echo "~~~ Cleaning up left-over container ${container}"
712
docker stop "$container"
813
done
914
fi
1015

16+
if ! is_windows && [[ "${BUILDKITE_PLUGIN_DOCKER_MOUNT_CHECKOUT:-true}" =~ ^(true|on|1)$ ]] && [[ "${BUILDKITE_PLUGIN_DOCKER_CHOWN:-false}" =~ ^(true|on|1)$ ]] ; then
17+
docker run --rm -v "$PWD":"$PWD" "${BUILDKITE_PLUGIN_DOCKER_CHOWN_IMAGE:-busybox}" chown -Rh "$(id -u):$(id -g)" "$PWD"
18+
fi
19+

plugin.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ configuration:
1111
type: array
1212
always-pull:
1313
type: boolean
14+
chown:
15+
type: boolean
16+
chown-image:
17+
type: string
1418
command:
1519
type: array
1620
cpus:

tests/pre-exit.bats

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env bats
2+
3+
load "${BATS_PLUGIN_PATH}/load.bash"
4+
5+
@test "Runs chown" {
6+
export BUILDKITE_PLUGIN_DOCKER_CHOWN=true
7+
8+
stub docker \
9+
"run --rm -v $PWD:$PWD busybox chown -Rh $(id -u):$(id -g) $PWD : echo cleaned"
10+
11+
run "$PWD"/hooks/pre-exit
12+
13+
assert_success
14+
assert_output --partial "cleaned"
15+
16+
unstub docker
17+
}
18+
19+
@test "Doesn't run if not configured" {
20+
unset BUILDKITE_PLUGIN_DOCKER_CHOWN
21+
22+
stub docker \
23+
"run --rm -v $PWD:$PWD busybox chown -Rh $(id -u):$(id -g) $PWD : echo cleaned"
24+
25+
run "$PWD"/hooks/pre-exit
26+
27+
assert_success
28+
refute_output --partial "cleaned"
29+
30+
unstub docker || true
31+
}
32+
33+
@test "Doesn't run if checkout not mounted" {
34+
export BUILDKITE_PLUGIN_DOCKER_CHOWN=true
35+
export BUILDKITE_PLUGIN_DOCKER_MOUNT_CHECKOUT=false
36+
37+
stub docker \
38+
"run --rm -v $PWD:$PWD busybox chown -Rh $(id -u):$(id -g) $PWD : echo cleaned"
39+
40+
run "$PWD"/hooks/pre-exit
41+
42+
assert_success
43+
refute_output --partial "cleaned"
44+
45+
unstub docker || true
46+
}
47+
48+
@test "Use custom image" {
49+
export BUILDKITE_PLUGIN_DOCKER_CHOWN=true
50+
export BUILDKITE_PLUGIN_DOCKER_CHOWN_IMAGE=some-image
51+
52+
stub docker \
53+
"run --rm -v $PWD:$PWD some-image chown -Rh $(id -u):$(id -g) $PWD : echo cleaned"
54+
55+
run "$PWD"/hooks/pre-exit
56+
57+
assert_success
58+
assert_output --partial "cleaned"
59+
60+
unstub docker
61+
}

0 commit comments

Comments
 (0)