Skip to content

Commit

Permalink
Merge pull request #372 from buildkite-plugins/toote_known_hosts
Browse files Browse the repository at this point in the history
`known_hosts` path customization
  • Loading branch information
pzeballos authored Feb 20, 2023
2 parents fe3522a + 8d01679 commit b584af1
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 37 deletions.
58 changes: 29 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The following pipeline will run `test.sh` inside a `app` service container using
steps:
- command: test.sh
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
run: app
```
Expand All @@ -28,7 +28,7 @@ through if you need:
steps:
- command: test.sh
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
run: app
config: docker-compose.tests.yml
env:
Expand All @@ -41,7 +41,7 @@ or multiple config files:
steps:
- command: test.sh
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
run: app
config:
- docker-compose.yml
Expand All @@ -56,7 +56,7 @@ env:
steps:
- command: test.sh
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
run: app
```

Expand All @@ -65,7 +65,7 @@ If you want to control how your command is passed to docker-compose, you can use
```yml
steps:
- plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
run: app
command: ["custom", "command", "values"]
```
Expand All @@ -79,15 +79,15 @@ steps:
- plugins:
- docker-login#v2.0.1:
username: xyz
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
build: app
image-repository: index.docker.io/myorg/myrepo
- wait
- command: test.sh
plugins:
- docker-login#v2.0.1:
username: xyz
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
run: app
```

Expand All @@ -104,7 +104,7 @@ steps:
- command: generate-dist.sh
artifact_paths: "dist/*"
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
run: app
```

Expand All @@ -122,7 +122,7 @@ steps:
- command: generate-dist.sh
artifact_paths: "dist/*"
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
run: app
volumes:
- "./dist:/app/dist"
Expand All @@ -146,7 +146,7 @@ this plugin offers a `environment` block of its own:
steps:
- command: generate-dist.sh
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
run: app
env:
- BUILDKITE_BUILD_NUMBER
Expand All @@ -164,7 +164,7 @@ Alternatively, you can have the plugin add all environment variables defined for
steps:
- command: use-vars.sh
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
run: app
propagate-environment: true
```
Expand All @@ -179,7 +179,7 @@ Alternatively, if you want to set build arguments when pre-building an image, th
steps:
- command: generate-dist.sh
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
build: app
image-repository: index.docker.io/myorg/myrepo
args:
Expand All @@ -196,7 +196,7 @@ If you have multiple steps that use the same service/image (such as steps that r
steps:
- label: ":docker: Build"
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
build: app
image-repository: index.docker.io/myorg/myrepo
Expand All @@ -206,7 +206,7 @@ steps:
command: test.sh
parallelism: 25
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
run: app
```

Expand All @@ -222,7 +222,7 @@ steps:
agents:
queue: docker-builder
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
build:
- app
- tests
Expand All @@ -234,7 +234,7 @@ steps:
command: test.sh
parallelism: 25
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
run: tests
```

Expand All @@ -246,7 +246,7 @@ If you want to push your Docker images ready for deployment, you can use the `pu
steps:
- label: ":docker: Push"
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
push: app
```

Expand All @@ -256,7 +256,7 @@ To push multiple images, you can use a list:
steps:
- label: ":docker: Push"
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
push:
- first-service
- second-service
Expand All @@ -268,7 +268,7 @@ If you want to push to a specific location (that's not defined as the `image` in
steps:
- label: ":docker: Push"
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
push:
- app:index.docker.io/myorg/myrepo/myapp
- app:index.docker.io/myorg/myrepo/myapp:latest
Expand All @@ -282,14 +282,14 @@ A newly spawned agent won't contain any of the docker caches for the first run w
steps:
- label: ":docker: Build an image"
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
build: app
image-repository: index.docker.io/myorg/myrepo
cache-from: app:index.docker.io/myorg/myrepo/myapp:latest
- wait
- label: ":docker: Push to final repository"
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
push:
- app:index.docker.io/myorg/myrepo/myapp
- app:index.docker.io/myorg/myrepo/myapp:latest
Expand All @@ -307,7 +307,7 @@ This plugin allows for the value of `cache-from` to be a string or a list. If it
steps:
- label: ":docker Build an image"
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
build: app
image-repository: index.docker.io/myorg/myrepo
separator-cache-from: "#"
Expand All @@ -317,7 +317,7 @@ steps:
- wait
- label: ":docker: Push to final repository"
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
push:
- app:myregistry:port/myrepo/myapp:my-branch
- app:myregistry:port/myrepo/myapp:latest
Expand All @@ -330,7 +330,7 @@ Adding a grouping tag to the end of a cache-from list item allows this plugin to
steps:
- label: ":docker: Build Intermediate Image"
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
build: myservice_intermediate # docker-compose.yml is the same as myservice but has `target: intermediate`
image-name: buildkite-build-${BUILDKITE_BUILD_NUMBER}
image-repository: index.docker.io/myorg/myrepo/myservice_intermediate
Expand All @@ -340,7 +340,7 @@ steps:
- wait
- label: ":docker: Build Final Image"
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
build: myservice
image-name: buildkite-build-${BUILDKITE_BUILD_NUMBER}
image-repository: index.docker.io/myorg/myrepo
Expand Down Expand Up @@ -384,7 +384,7 @@ A basic pipeline similar to the following:
steps:
- label: ":docker: Run & Push"
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
run: myservice
push: myservice
```
Expand All @@ -399,7 +399,7 @@ A basic pipeline similar to the following:
steps:
- label: ":docker: Build & Push"
plugins:
- docker-compose#v4.9.0:
- docker-compose#v4.10.0:
build: myservice
push: myservice
```
Expand Down Expand Up @@ -486,9 +486,9 @@ Whether to match the user ID and group ID for the container user to the user ID

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.

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

Whether to automatically mount the ssh-agent socket from the host agent machine into the container (at `/ssh-agent`and `/root/.ssh/known_hosts` respectively), allowing git operations to work correctly.
Whether to mount the ssh-agent socket (at `/ssh-agent`) from the host agent machine into the container or not. Instead of just `true` or `false`, you can specify absolute path in the container for the home directory of the user used to run on which the agent's `.ssh/known_hosts` will be mounted (by default, `/root`).

Default: `false`

Expand Down
16 changes: 9 additions & 7 deletions commands/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ run_service="$(plugin_read_config RUN)"
container_name="$(docker_compose_project_name)_${run_service}_build_${BUILDKITE_BUILD_NUMBER}"
override_file="docker-compose.buildkite-${BUILDKITE_BUILD_NUMBER}-override.yml"
pull_retries="$(plugin_read_config PULL_RETRIES "0")"
mount_ssh_agent=''
mount_checkout="$(plugin_read_config MOUNT_CHECKOUT "false")"
workdir=''

Expand Down Expand Up @@ -193,23 +192,26 @@ if [[ -n "$(plugin_read_config ENTRYPOINT)" ]] ; then
fi

# Mount ssh-agent socket and known_hosts
if [[ "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_MOUNT_SSH_AGENT:-$mount_ssh_agent}" =~ ^(true|on|1)$ ]] ; then
if [[ ! "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_MOUNT_SSH_AGENT:-false}" = 'false' ]] ; then
if [[ -z "${SSH_AUTH_SOCK:-}" ]] ; then
echo "+++ 🚨 \$SSH_AUTH_SOCK isn't set, has ssh-agent started?"
exit 1
fi
if [[ ! -S "${SSH_AUTH_SOCK}" ]] ; then
echo "+++ 🚨 There isn't any file at ${SSH_AUTH_SOCK}, has ssh-agent started?"
echo "+++ 🚨 The file at ${SSH_AUTH_SOCK} does not exist or is not a socket, was ssh-agent started?"
exit 1
fi
if [[ ! -S "${SSH_AUTH_SOCK}" ]] ; then
echo "+++ 🚨 The file at ${SSH_AUTH_SOCK} isn't a socket, has ssh-agent started?"
exit 1

if [[ "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_MOUNT_SSH_AGENT:-''}" =~ ^(true|on|1)$ ]]; then
MOUNT_PATH=/root
else
MOUNT_PATH="${BUILDKITE_PLUGIN_DOCKER_COMPOSE_MOUNT_SSH_AGENT}"
fi

run_params+=(
"-e" "SSH_AUTH_SOCK=/ssh-agent"
"-v" "${SSH_AUTH_SOCK}:/ssh-agent"
"-v" "${HOME}/.ssh/known_hosts:/root/.ssh/known_hosts"
"-v" "${HOME}/.ssh/known_hosts:${MOUNT_PATH}/.ssh/known_hosts"
)
fi

Expand Down
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ configuration:
mount-buildkite-agent:
type: boolean
mount-ssh-agent:
type: boolean
type: [ boolean, string ]
mount-checkout:
type: [ boolean, string ]
no-cache:
Expand Down
35 changes: 35 additions & 0 deletions tests/run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1204,9 +1204,44 @@ export BUILDKITE_JOB_ID=1111

run "$PWD"/hooks/command

assert_success

kill %1

assert_output --partial "built myservice"
assert_output --partial "ran myservice"
unstub docker-compose
unstub buildkite-agent
}

@test "Run with mount-ssh-agent on particular folder" {
export SSH_AUTH_SOCK=/tmp/ssh_auth_sock
export BUILDKITE_JOB_ID=1111
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice
export BUILDKITE_PIPELINE_SLUG=test
export BUILDKITE_BUILD_NUMBER=1
export BUILDKITE_COMMAND="echo hello world"
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CHECK_LINKED_CONTAINERS=false
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CLEANUP=false
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_MOUNT_SSH_AGENT=/tmp/test

stub docker-compose \
"-f docker-compose.yml -p buildkite1111 build --pull myservice : echo built myservice" \
"-f docker-compose.yml -p buildkite1111 up -d --scale myservice=0 myservice : echo ran myservice dependencies" \
"-f docker-compose.yml -p buildkite1111 run --name buildkite1111_myservice_build_1 --rm -e SSH_AUTH_SOCK=/ssh-agent -v /tmp/ssh_auth_sock:/ssh-agent -v /root/.ssh/known_hosts:/tmp/test/.ssh/known_hosts myservice /bin/sh -e -c 'echo hello world' : echo ran myservice"

stub buildkite-agent \
"meta-data exists docker-compose-plugin-built-image-tag-myservice : exit 1"

apk add netcat-openbsd
nc -lkvU $SSH_AUTH_SOCK &

run "$PWD"/hooks/command

assert_success

kill %1

assert_output --partial "built myservice"
assert_output --partial "ran myservice"
unstub docker-compose
Expand Down
33 changes: 33 additions & 0 deletions tests/v2/run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,39 @@ export BUILDKITE_JOB_ID=1111
unstub buildkite-agent
}

@test "Run with mount-ssh-agent on particular folder" {
export SSH_AUTH_SOCK=/tmp/ssh_auth_sock
export BUILDKITE_JOB_ID=1111
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice
export BUILDKITE_PIPELINE_SLUG=test
export BUILDKITE_BUILD_NUMBER=1
export BUILDKITE_COMMAND="echo hello world"
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CHECK_LINKED_CONTAINERS=false
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CLEANUP=false
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_MOUNT_SSH_AGENT=/tmp/test

stub docker \
"compose -f docker-compose.yml -p buildkite1111 build --pull myservice : echo built myservice" \
"compose -f docker-compose.yml -p buildkite1111 up -d --scale myservice=0 myservice : echo ran myservice dependencies" \
"compose -f docker-compose.yml -p buildkite1111 run --name buildkite1111_myservice_build_1 --rm -e SSH_AUTH_SOCK=/ssh-agent -v /tmp/ssh_auth_sock:/ssh-agent -v /root/.ssh/known_hosts:/tmp/test/.ssh/known_hosts myservice /bin/sh -e -c 'echo hello world' : echo ran myservice"

stub buildkite-agent \
"meta-data exists docker-compose-plugin-built-image-tag-myservice : exit 1"

apk add netcat-openbsd
nc -lkvU $SSH_AUTH_SOCK &

run "$PWD"/hooks/command

kill %1

assert_success
assert_output --partial "built myservice"
assert_output --partial "ran myservice"
unstub docker
unstub buildkite-agent
}

@test "Run without mount-checkout doesn't set volume" {
export BUILDKITE_BUILD_NUMBER=1
export BUILDKITE_JOB_ID=1111
Expand Down

0 comments on commit b584af1

Please sign in to comment.