From 3710e14baa9a8bef0fa65866f9aef37570ff0b5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Sun, 13 Nov 2022 21:31:55 -0300 Subject: [PATCH 1/3] Added wait option documentation and to spec --- README.md | 6 ++++++ plugin.yml | 1 + 2 files changed, 7 insertions(+) diff --git a/README.md b/README.md index 48b6d5e0..163998d1 100644 --- a/README.md +++ b/README.md @@ -572,6 +572,12 @@ If set to false, doesn't start linked services. The default is `true`. +### `wait` (optional, run only) + +Whether to wait for dependencies to be up (and healthy if possible) when starting them up. It translates to using [`--wait` in the docker-compose up] command. + +Defaults to `false`. + ### `ansi` (optional, run only) If set to false, disables the ansi output from containers. diff --git a/plugin.yml b/plugin.yml index 71025495..edb40f51 100644 --- a/plugin.yml +++ b/plugin.yml @@ -128,4 +128,5 @@ configuration: use-aliases: [ run ] user: [ run ] volumes: [ run ] + wait: [ run ] workdir: [ run ] From acf42ba9ad97adea942dd16988941e273aee5714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Sun, 13 Nov 2022 21:32:17 -0300 Subject: [PATCH 2/3] Implemented --wait (and simplified a bit the code) --- commands/run.sh | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index cc0f36c0..d3fb45a4 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -262,16 +262,17 @@ elif [[ ! -f "$override_file" ]]; then run_docker_compose build "${build_params[@]}" "$run_service" fi +up_params+=("up") # this ensures that the array has elements to avoid issues with bash 4.3 + +if [[ "$(plugin_read_config WAIT "false")" == "true" ]] ; then + up_params+=("--wait") +fi + dependency_exitcode=0 if [[ "$(plugin_read_config DEPENDENCIES "true")" == "true" ]] ; then - # Start up service dependencies in a different header to keep the main run with less noise echo "~~~ :docker: Starting dependencies" - if [[ ${#up_params[@]} -gt 0 ]] ; then - run_docker_compose "${up_params[@]}" up -d --scale "${run_service}=0" "${run_service}" || dependency_exitcode=$? - else - run_docker_compose up -d --scale "${run_service}=0" "${run_service}" || dependency_exitcode=$? - fi + run_docker_compose "${up_params[@]}" -d --scale "${run_service}=0" "${run_service}" || dependency_exitcode=$? fi if [[ $dependency_exitcode -ne 0 ]] ; then From 25c1c33043bbfc7253c6fecaa1fec8cd48c0ff2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Sun, 13 Nov 2022 21:32:37 -0300 Subject: [PATCH 3/3] Added tests for new feature --- tests/run.bats | 28 ++++++++++++++++++++++++++++ tests/v2/run.bats | 28 ++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/tests/run.bats b/tests/run.bats index 4f4918c8..5f3f35cf 100644 --- a/tests/run.bats +++ b/tests/run.bats @@ -1356,3 +1356,31 @@ export BUILDKITE_JOB_ID=1111 unstub docker-compose unstub buildkite-agent } + +@test "Run waiting for dependencies" { + export BUILDKITE_BUILD_NUMBER=1 + export BUILDKITE_COMMAND="echo hello world" + export BUILDKITE_JOB_ID=1111 + export BUILDKITE_PIPELINE_SLUG=test + + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CHECK_LINKED_CONTAINERS=false + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_WAIT=true + + stub docker-compose \ + "-f docker-compose.yml -p buildkite1111 build --pull myservice : echo built myservice" \ + "-f docker-compose.yml -p buildkite1111 up --wait -d --scale myservice=0 : echo ran myservice dependencies" \ + "-f docker-compose.yml -p buildkite1111 run --name buildkite1111_myservice_build_1 --rm 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" + + run "$PWD"/hooks/command + + assert_success + assert_output --partial "Starting dependencies" + assert_output --partial "ran myservice" + + unstub docker-compose + unstub buildkite-agent +} \ No newline at end of file diff --git a/tests/v2/run.bats b/tests/v2/run.bats index 46abd364..316b45bb 100644 --- a/tests/v2/run.bats +++ b/tests/v2/run.bats @@ -1232,3 +1232,31 @@ export BUILDKITE_JOB_ID=1111 unstub docker unstub buildkite-agent } + +@test "Run waiting for dependencies" { + export BUILDKITE_BUILD_NUMBER=1 + export BUILDKITE_COMMAND="echo hello world" + export BUILDKITE_JOB_ID=1111 + export BUILDKITE_PIPELINE_SLUG=test + + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CHECK_LINKED_CONTAINERS=false + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_WAIT=true + + stub docker \ + "compose -f docker-compose.yml -p buildkite1111 build --pull myservice : echo built myservice" \ + "compose -f docker-compose.yml -p buildkite1111 up --wait -d --scale myservice=0 : echo ran myservice dependencies" \ + "compose -f docker-compose.yml -p buildkite1111 run --name buildkite1111_myservice_build_1 --rm 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" + + run "$PWD"/hooks/command + + assert_success + assert_output --partial "built myservice" + assert_output --partial "ran myservice" + + unstub docker + unstub buildkite-agent +} \ No newline at end of file