Skip to content

Commit

Permalink
Merge pull request #275 from buildkite-plugins/toote_job_api_mounting
Browse files Browse the repository at this point in the history
Job api mounting
  • Loading branch information
pzeballos authored Aug 6, 2024
2 parents 41ca069 + 69b5f36 commit 7754095
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ Set to `true` if you want to enable and are sure that the binary running in the

Default: `false`

**Important:** enabling this option will share `BUILDKITE_AGENT_TOKEN` environment variable (and others) with the container
**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.

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

Expand Down
8 changes: 8 additions & 0 deletions commands/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ if [[ -n "${BUILDKITE_AGENT_BINARY_PATH:-}" ]] ; then
)
fi

if [[ -n "${BUILDKITE_AGENT_JOB_API_SOCKET:-}" ]] ; then
args+=(
"--env" "BUILDKITE_AGENT_JOB_API_SOCKET"
"--env" "BUILDKITE_AGENT_JOB_API_TOKEN"
"--volume" "$BUILDKITE_AGENT_JOB_API_SOCKET:$BUILDKITE_AGENT_JOB_API_SOCKET"
)
fi

# Parse extra env vars and add them to the docker args
while IFS='=' read -r name _ ; do
if [[ $name =~ ^(BUILDKITE_PLUGIN_DOCKER_ENVIRONMENT_[0-9]+) ]] ; then
Expand Down
23 changes: 23 additions & 0 deletions tests/command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,29 @@ setup() {
unstub buildkite-agent || true
}

@test "Runs BUILDKITE_COMMAND with mount-buildkite-agent enabled and Job API" {
export BUILDKITE_PLUGIN_DOCKER_MOUNT_BUILDKITE_AGENT=true
export BUILDKITE_AGENT_JOB_API_SOCKET=/special/path
export BUILDKITE_COMMAND="pwd"

stub docker \
"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}"

# only for the command to exist
stub buildkite-agent \
" : exit 1"

run "$PWD"/hooks/command

assert_success
refute_output --partial "🚨 Failed to find buildkite-agent"
assert_output --partial "ran command in docker"
assert_output --partial "/bin/buildkite-agent:/usr/bin/buildkite-agent" # check agent is mounted

unstub docker
unstub buildkite-agent || true
}

@test "Runs BUILDKITE_COMMAND with volumes" {
export BUILDKITE_PLUGIN_DOCKER_WORKDIR=/app
export BUILDKITE_PLUGIN_DOCKER_VOLUMES_0=/var/run/docker.sock:/var/run/docker.sock
Expand Down

0 comments on commit 7754095

Please sign in to comment.