Skip to content

Commit 7754095

Browse files
authored
Merge pull request #275 from buildkite-plugins/toote_job_api_mounting
Job api mounting
2 parents 41ca069 + 69b5f36 commit 7754095

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ Set to `true` if you want to enable and are sure that the binary running in the
327327

328328
Default: `false`
329329

330-
**Important:** enabling this option will share `BUILDKITE_AGENT_TOKEN` environment variable (and others) with the container
330+
**Important:** enabling this option will share the `BUILDKITE_AGENT_TOKEN` and `BUILDKITE_AGENT_JOB_API_TOKEN` environment variables (and other related ones) with the container if present.
331331

332332
### `mount-ssh-agent` (optional, boolean or string)
333333

commands/run.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,14 @@ if [[ -n "${BUILDKITE_AGENT_BINARY_PATH:-}" ]] ; then
223223
)
224224
fi
225225

226+
if [[ -n "${BUILDKITE_AGENT_JOB_API_SOCKET:-}" ]] ; then
227+
args+=(
228+
"--env" "BUILDKITE_AGENT_JOB_API_SOCKET"
229+
"--env" "BUILDKITE_AGENT_JOB_API_TOKEN"
230+
"--volume" "$BUILDKITE_AGENT_JOB_API_SOCKET:$BUILDKITE_AGENT_JOB_API_SOCKET"
231+
)
232+
fi
233+
226234
# Parse extra env vars and add them to the docker args
227235
while IFS='=' read -r name _ ; do
228236
if [[ $name =~ ^(BUILDKITE_PLUGIN_DOCKER_ENVIRONMENT_[0-9]+) ]] ; then

tests/command.bats

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,29 @@ setup() {
119119
unstub buildkite-agent || true
120120
}
121121

122+
@test "Runs BUILDKITE_COMMAND with mount-buildkite-agent enabled and Job API" {
123+
export BUILDKITE_PLUGIN_DOCKER_MOUNT_BUILDKITE_AGENT=true
124+
export BUILDKITE_AGENT_JOB_API_SOCKET=/special/path
125+
export BUILDKITE_COMMAND="pwd"
126+
127+
stub docker \
128+
"run -t -i --rm --init --volume $PWD:/workdir --workdir /workdir --env BUILDKITE_JOB_ID --env BUILDKITE_BUILD_ID --env BUILDKITE_AGENT_ACCESS_TOKEN --volume \* --env BUILDKITE_AGENT_JOB_API_SOCKET --env BUILDKITE_AGENT_JOB_API_TOKEN --volume \* --label com.buildkite.job-id=1-2-3-4 image:tag /bin/sh -e -c 'pwd' : echo ran command in docker with buildkite agent mounted at \${23}"
129+
130+
# only for the command to exist
131+
stub buildkite-agent \
132+
" : exit 1"
133+
134+
run "$PWD"/hooks/command
135+
136+
assert_success
137+
refute_output --partial "🚨 Failed to find buildkite-agent"
138+
assert_output --partial "ran command in docker"
139+
assert_output --partial "/bin/buildkite-agent:/usr/bin/buildkite-agent" # check agent is mounted
140+
141+
unstub docker
142+
unstub buildkite-agent || true
143+
}
144+
122145
@test "Runs BUILDKITE_COMMAND with volumes" {
123146
export BUILDKITE_PLUGIN_DOCKER_WORKDIR=/app
124147
export BUILDKITE_PLUGIN_DOCKER_VOLUMES_0=/var/run/docker.sock:/var/run/docker.sock

0 commit comments

Comments
 (0)