From 1e79bb2e1ebb56916842666b03b1c5ce4fa337c1 Mon Sep 17 00:00:00 2001 From: Francois Campbell Date: Sun, 16 Oct 2022 18:49:04 -0700 Subject: [PATCH 1/5] Add mount-checkout support --- README.md | 10 +++++++++- commands/run.sh | 22 ++++++++++++++++++++-- plugin.yml | 2 ++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f939336b..88fe261d 100644 --- a/README.md +++ b/README.md @@ -468,7 +468,9 @@ Completely avoid running any `pull` command. Images being used will need to be p ### `workdir` (optional, run only) -Specify the container working directory via `docker-compose run --workdir`. +Specify the container working directory via `docker-compose run --workdir`. The default is `/workdir`. This path is also used by `mount-checkout` to determine where to mount the checkout in the container. + +Example: `/app` ### `user` (optional, run only) @@ -492,6 +494,12 @@ Whether to automatically mount the `buildkite-agent` binary and associated envir Default: `false` +### `mount-checkout` (optional, boolean) + +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. + +Default: `true` + ### `pull-retries` (optional) A number of times to retry failed docker pull. Defaults to 0. diff --git a/commands/run.sh b/commands/run.sh index b78f01a1..56a215d8 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -9,6 +9,7 @@ container_name="$(docker_compose_project_name)_${run_service}_build_${BUILDKITE_ 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 "true")" expand_headers_on_error() { echo "^^^ +++" @@ -115,10 +116,16 @@ if [[ -n "${BUILDKITE_REPO_MIRROR:-}" ]]; then fi tty_default='true' +workdir_default="/workdir" +pwd_default="$PWD" # Set operating system specific defaults if is_windows ; then tty_default='false' + workdir_default="C:\\workdir" + # escaping /C is a necessary workaround for an issue with Git for Windows 2.24.1.2 + # https://github.com/git-for-windows/git/issues/2442 + pwd_default="$(cmd.exe //C "echo %CD%")" fi # Optionally disable allocating a TTY @@ -131,8 +138,19 @@ if [[ "$(plugin_read_config DEPENDENCIES "true")" == "false" ]] ; then run_params+=(--no-deps) fi -if [[ -n "$(plugin_read_config WORKDIR)" ]] ; then - run_params+=("--workdir=$(plugin_read_config WORKDIR)") +workdir='' + +if [[ -n "$(plugin_read_config WORKDIR)" ]] || [[ "${mount_checkout}" == "true" ]]; then + workdir="$(plugin_read_config WORKDIR "$workdir_default")" +fi + +if [[ -n "${workdir}" ]] ; then + run_params+=("--workdir=${workdir}") +fi + +# By default, mount $PWD onto $WORKDIR +if [[ "${mount_checkout}" == "true" ]] ; then + run_params+=("-v" "${pwd_default}:${workdir}") fi # Can't set both user and propagate-uid-gid diff --git a/plugin.yml b/plugin.yml index c2136ecb..8f855c55 100644 --- a/plugin.yml +++ b/plugin.yml @@ -59,6 +59,8 @@ configuration: type: boolean mount-ssh-agent: type: boolean + mount-checkout: + type: boolean no-cache: type: boolean propagate-environment: From a2dc3f60600c93482da37942d2dce97d815c2e38 Mon Sep 17 00:00:00 2001 From: Francois Campbell Date: Mon, 17 Oct 2022 13:41:26 -0700 Subject: [PATCH 2/5] PR comments, dont make it on by default --- README.md | 4 ++-- commands/run.sh | 2 +- plugin.yml | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 88fe261d..afaab7d5 100644 --- a/README.md +++ b/README.md @@ -494,11 +494,11 @@ Whether to automatically mount the `buildkite-agent` binary and associated envir Default: `false` -### `mount-checkout` (optional, boolean) +### `mount-checkout` (optional, run-only, boolean) 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. -Default: `true` +Default: `false` ### `pull-retries` (optional) diff --git a/commands/run.sh b/commands/run.sh index 56a215d8..5784ed47 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -9,7 +9,7 @@ container_name="$(docker_compose_project_name)_${run_service}_build_${BUILDKITE_ 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 "true")" +mount_checkout="$(plugin_read_config MOUNT_CHECKOUT "false")" expand_headers_on_error() { echo "^^^ +++" diff --git a/plugin.yml b/plugin.yml index 8f855c55..b511865f 100644 --- a/plugin.yml +++ b/plugin.yml @@ -117,6 +117,7 @@ configuration: image-name: [ build ] leave-volumes: [ run ] mount-buildkite-agent: [ run ] + mount-checkout: [ run ] propagate-uid-gid: [ run ] pull: [ run ] push-retries: [ push ] From d58a85c164c7ee014283ba27646fa1a2f3df46ed Mon Sep 17 00:00:00 2001 From: Francois Campbell Date: Wed, 19 Oct 2022 09:08:50 -0700 Subject: [PATCH 3/5] Add tests --- tests/run.bats | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/tests/run.bats b/tests/run.bats index 2b4ef823..9e9aea9f 100644 --- a/tests/run.bats +++ b/tests/run.bats @@ -1159,3 +1159,84 @@ export BUILDKITE_JOB_ID=1111 unstub docker-compose unstub buildkite-agent } + +@test "mount-checkout is not enabled by default" { + 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=pwd + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CHECK_LINKED_CONTAINERS=false + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CLEANUP=false + + stub docker-compose \ + "-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml pull myservice : echo pulled myservice" \ + "-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml up -d --scale myservice=0 myservice : echo started dependencies for myservice" \ + "-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml run --name buildkite1111_myservice_build_1 --rm myservice /bin/sh -e -c 'pwd' : echo ran myservice without mount-checkout" + + stub buildkite-agent \ + "meta-data exists docker-compose-plugin-built-image-tag-myservice : exit 0" \ + "meta-data get docker-compose-plugin-built-image-tag-myservice : echo myimage" + + run "$PWD"/hooks/command + + assert_success + assert_output --partial "ran myservice without mount-checkout" + unstub docker-compose + unstub buildkite-agent +} + +@test "Run with mount-checkout set with default workdir" { + 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=pwd + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CHECK_LINKED_CONTAINERS=false + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CLEANUP=false + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_MOUNT_CHECKOUT=true + + stub docker-compose \ + "-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml pull myservice : echo pulled myservice" \ + "-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml up -d --scale myservice=0 myservice : echo started dependencies for myservice" \ + "-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml run --name buildkite1111_myservice_build_1 --workdir=/workdir -v $PWD:/workdir --rm myservice /bin/sh -e -c 'pwd' : echo ran myservice with mount-checkout" + + stub buildkite-agent \ + "meta-data exists docker-compose-plugin-built-image-tag-myservice : exit 0" \ + "meta-data get docker-compose-plugin-built-image-tag-myservice : echo myimage" + + run "$PWD"/hooks/command + + assert_success + assert_output --partial "ran myservice with mount-checkout" + unstub docker-compose + unstub buildkite-agent +} + +@test "Run with mount-checkout set with custom workdir" { + 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=pwd + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CHECK_LINKED_CONTAINERS=false + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CLEANUP=false + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_MOUNT_CHECKOUT=true + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_WORKDIR="/custom_workdir" + + stub docker-compose \ + "-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml pull myservice : echo pulled myservice" \ + "-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml up -d --scale myservice=0 myservice : echo started dependencies for myservice" \ + "-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml run --name buildkite1111_myservice_build_1 --workdir=$BUILDKITE_PLUGIN_DOCKER_COMPOSE_WORKDIR -v $PWD:$BUILDKITE_PLUGIN_DOCKER_COMPOSE_WORKDIR --rm myservice /bin/sh -e -c 'pwd' : echo ran myservice with mount-checkout" + + stub buildkite-agent \ + "meta-data exists docker-compose-plugin-built-image-tag-myservice : exit 0" \ + "meta-data get docker-compose-plugin-built-image-tag-myservice : echo myimage" + + run "$PWD"/hooks/command + + assert_success + assert_output --partial "ran myservice with mount-checkout" + unstub docker-compose + unstub buildkite-agent +} From 5836d15a38b20cfb33a1a9dce319722af7944d19 Mon Sep 17 00:00:00 2001 From: Francois Campbell Date: Wed, 19 Oct 2022 15:32:19 -0700 Subject: [PATCH 4/5] edit test to set explicit false --- tests/run.bats | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/run.bats b/tests/run.bats index 9e9aea9f..3f8e2e53 100644 --- a/tests/run.bats +++ b/tests/run.bats @@ -1160,7 +1160,7 @@ export BUILDKITE_JOB_ID=1111 unstub buildkite-agent } -@test "mount-checkout is not enabled by default" { +@test "Run without mount-checkout doesn't set volume" { export BUILDKITE_JOB_ID=1111 export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice export BUILDKITE_PIPELINE_SLUG=test @@ -1168,6 +1168,7 @@ export BUILDKITE_JOB_ID=1111 export BUILDKITE_COMMAND=pwd export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CHECK_LINKED_CONTAINERS=false export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CLEANUP=false + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_MOUNT_CHECKOUT=false stub docker-compose \ "-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml pull myservice : echo pulled myservice" \ From d969fd86ce41bd41cf385aafbc4bdb15e5072c07 Mon Sep 17 00:00:00 2001 From: Francois Campbell Date: Wed, 19 Oct 2022 21:49:24 -0700 Subject: [PATCH 5/5] Update README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Matías Bellone --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index afaab7d5..4b5054ea 100644 --- a/README.md +++ b/README.md @@ -468,7 +468,7 @@ Completely avoid running any `pull` command. Images being used will need to be p ### `workdir` (optional, run only) -Specify the container working directory via `docker-compose run --workdir`. The default is `/workdir`. This path is also used by `mount-checkout` to determine where to mount the checkout in the container. +Specify the container working directory via `docker-compose run --workdir`. This option is also used by [`mount-checkout`](#mount-checkout-optional-run-only-boolean) to determine where to mount the checkout in the container. Example: `/app`