From 6b5dd30390d726d4cc04f9a4c67669663bce78a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Mon, 12 Dec 2022 21:53:36 -0300 Subject: [PATCH 1/7] Added compatibility option to plugin spec and documentation --- README.md | 12 ++++++++++++ plugin.yml | 2 ++ 2 files changed, 14 insertions(+) diff --git a/README.md b/README.md index f406117c..b86aec0d 100644 --- a/README.md +++ b/README.md @@ -602,6 +602,18 @@ If set to true, docker compose will remove the primary container after run. Equi The default is `true`. +### `compatibility` (optional, run only) + +If set to true, docker compose will run the `up` command with compatibility mode. Equivalent to `--compatibility` in docker-compose. + +The default is `false`. + +Note that [the effect of this option changes depending on your docker compose CLI version](https://docs.docker.com/compose/cli-command-compatibility/#flags-that-will-not-be-implemented): +* in v1 it translates (composefile) v3 deploy keys to their non-swarm (composefile) v2 equivalents +* in v2 it will revert some behaviour to v1 as well, including (but not limited to): + - [Character separator for container names](https://github.com/docker/compose/blob/a0acc20d883ce22b8b0c65786e3bea1328809bbd/cmd/compose/compose.go#L181) + - [Not normalizing compose models (when running `config`)](https://github.com/docker/compose/blob/2e7644ff21f9ca0ea6fb5e8d41d4f6af32cd7e20/cmd/compose/convert.go#L69) (but it shouldn't apply here because it is only used in the `up` command) + ### `entrypoint` (optional, run only) Sets the `--entrypoint` argument when running `docker-compose`. diff --git a/plugin.yml b/plugin.yml index 238b5041..4741b1e7 100644 --- a/plugin.yml +++ b/plugin.yml @@ -27,6 +27,8 @@ configuration: cache-from: type: [ string, array ] minimum: 1 + compatibility: + type: boolean cli-version: type: string enum: From 706d9dcd04896415713fcfc8304df2c66605b4d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Mon, 12 Dec 2022 21:54:00 -0300 Subject: [PATCH 2/7] Implemented compatibility flag when running compose up --- commands/run.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/commands/run.sh b/commands/run.sh index c0008df7..a5618655 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -182,6 +182,11 @@ if [[ "$(plugin_read_config RM "true")" == "true" ]]; then run_params+=(--rm) fi +# Enable compatibility mode for v3 files +if [[ "$(plugin_read_config COMPATIBILITY "false")" == "true" ]]; then + run_params+=(--compatibility) +fi + # Optionally sets --entrypoint if [[ -n "$(plugin_read_config ENTRYPOINT)" ]] ; then run_params+=(--entrypoint) From be45f2215a0ff6d8b723bf9678e59ecb196ff266 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Mon, 12 Dec 2022 21:54:20 -0300 Subject: [PATCH 3/7] Added tests for v1 --- tests/run.bats | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/run.bats b/tests/run.bats index f50d73a3..48a39d96 100644 --- a/tests/run.bats +++ b/tests/run.bats @@ -612,6 +612,33 @@ export BUILDKITE_JOB_ID=1111 unstub buildkite-agent } +@test "Run with compatibility mode" { + export BUILDKITE_BUILD_NUMBER=1 + export BUILDKITE_COMMAND=pwd + export BUILDKITE_JOB_ID=1111 + export BUILDKITE_PIPELINE_SLUG=test + + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CLEANUP=false + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_COMPATIBILITY=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 --rm --compatibility myservice /bin/sh -e -c 'pwd' : echo ran myservice with use aliases output" + + 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 use aliases output" + unstub docker-compose + unstub buildkite-agent +} + @test "Run with a volumes option" { export BUILDKITE_JOB_ID=1111 export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice From b2bbc9760a8c4734faa0f65675d720e2b59d07b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Tue, 13 Dec 2022 03:29:02 -0300 Subject: [PATCH 4/7] Moved implementation to shared command as it is a global flag --- commands/run.sh | 5 ----- lib/shared.bash | 5 +++++ 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/commands/run.sh b/commands/run.sh index a5618655..c0008df7 100755 --- a/commands/run.sh +++ b/commands/run.sh @@ -182,11 +182,6 @@ if [[ "$(plugin_read_config RM "true")" == "true" ]]; then run_params+=(--rm) fi -# Enable compatibility mode for v3 files -if [[ "$(plugin_read_config COMPATIBILITY "false")" == "true" ]]; then - run_params+=(--compatibility) -fi - # Optionally sets --entrypoint if [[ -n "$(plugin_read_config ENTRYPOINT)" ]] ; then run_params+=(--entrypoint) diff --git a/lib/shared.bash b/lib/shared.bash index 583cb122..986eaf9a 100644 --- a/lib/shared.bash +++ b/lib/shared.bash @@ -200,6 +200,11 @@ function run_docker_compose() { command+=(--no-ansi) fi + # Enable compatibility mode for v3 files + if [[ "$(plugin_read_config COMPATIBILITY "false")" == "true" ]]; then + command+=(--compatibility) + fi + for file in $(docker_compose_config_files) ; do command+=(-f "$file") done From a581b8ef067196c4c04d7bdccd883c808212119a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Tue, 13 Dec 2022 03:30:30 -0300 Subject: [PATCH 5/7] Added doc clarification and push compatibility due to separator --- README.md | 4 ++-- lib/push.bash | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b86aec0d..2d7bae74 100644 --- a/README.md +++ b/README.md @@ -604,7 +604,7 @@ The default is `true`. ### `compatibility` (optional, run only) -If set to true, docker compose will run the `up` command with compatibility mode. Equivalent to `--compatibility` in docker-compose. +If set to true, all docker compose commands will rum with compatibility mode. Equivalent to `--compatibility` in docker-compose. The default is `false`. @@ -612,7 +612,7 @@ Note that [the effect of this option changes depending on your docker compose CL * in v1 it translates (composefile) v3 deploy keys to their non-swarm (composefile) v2 equivalents * in v2 it will revert some behaviour to v1 as well, including (but not limited to): - [Character separator for container names](https://github.com/docker/compose/blob/a0acc20d883ce22b8b0c65786e3bea1328809bbd/cmd/compose/compose.go#L181) - - [Not normalizing compose models (when running `config`)](https://github.com/docker/compose/blob/2e7644ff21f9ca0ea6fb5e8d41d4f6af32cd7e20/cmd/compose/convert.go#L69) (but it shouldn't apply here because it is only used in the `up` command) + - [Not normalizing compose models (when running `config`)](https://github.com/docker/compose/blob/2e7644ff21f9ca0ea6fb5e8d41d4f6af32cd7e20/cmd/compose/convert.go#L69) ### `entrypoint` (optional, run only) diff --git a/lib/push.bash b/lib/push.bash index d69ee8fa..1025c974 100644 --- a/lib/push.bash +++ b/lib/push.bash @@ -22,7 +22,7 @@ default_compose_image_for_service() { local service="$1" local separator="_" - if [[ "$(plugin_read_config CLI_VERSION "1")" == "2" ]] ; then + if [[ "$(plugin_read_config CLI_VERSION "1")" == "2" ]] && [[ "$(plugin_read_config COMPATIBILITY "false")" != "true" ]] ; then separator="-" fi From b272e0daad8ed92811b1fdfdadb5bfeab1d12e05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Tue, 13 Dec 2022 03:31:07 -0300 Subject: [PATCH 6/7] Updated test --- tests/run.bats | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/run.bats b/tests/run.bats index 48a39d96..fe7fa648 100644 --- a/tests/run.bats +++ b/tests/run.bats @@ -623,9 +623,9 @@ export BUILDKITE_JOB_ID=1111 export BUILDKITE_PLUGIN_DOCKER_COMPOSE_COMPATIBILITY=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 --rm --compatibility myservice /bin/sh -e -c 'pwd' : echo ran myservice with use aliases output" + "--compatibility -f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml pull myservice : echo pulled myservice" \ + "--compatibility -f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml up -d --scale myservice=0 myservice : echo started dependencies for myservice" \ + "--compatibility -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 with use aliases output" stub buildkite-agent \ "meta-data exists docker-compose-plugin-built-image-tag-myservice : exit 0" \ From 211da9018b3e213589cda82cd56f0df713625b36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20A=2E=20Bellone?= Date: Tue, 13 Dec 2022 03:42:49 -0300 Subject: [PATCH 7/7] Added v2 tests --- tests/v2/push.bats | 30 ++++++++++++++++++++++++++++++ tests/v2/run.bats | 28 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/tests/v2/push.bats b/tests/v2/push.bats index d0ef5ae1..f6d109af 100644 --- a/tests/v2/push.bats +++ b/tests/v2/push.bats @@ -96,6 +96,36 @@ setup_file() { unstub buildkite-agent } +@test "Push a prebuilt image with a repository and a tag in compatibility mode" { + export BUILDKITE_BUILD_NUMBER=1 + export BUILDKITE_JOB_ID=1111 + export BUILDKITE_PIPELINE_SLUG=test + + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_PUSH=myservice:my.repository/myservice:llamas + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_COMPATIBILITY=true + + stub docker \ + "compose --compatibility -f docker-compose.yml -p buildkite1111 config : echo blah" \ + "pull myimage : echo pulled prebuilt image" \ + "tag myimage buildkite1111_myservice : echo " \ + "tag buildkite1111_myservice my.repository/myservice:llamas : echo tagged image" \ + "push my.repository/myservice:llamas : echo pushed myservice" + + + 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 "pulled prebuilt image" + assert_output --partial "tagged image" + assert_output --partial "pushed myservice" + unstub docker + unstub buildkite-agent +} + @test "Push a prebuilt image with an invalid tag" { export BUILDKITE_JOB_ID=1111 export BUILDKITE_PLUGIN_DOCKER_COMPOSE_PUSH=myservice:my.repository/myservice:-llamas diff --git a/tests/v2/run.bats b/tests/v2/run.bats index 711019df..58fc252e 100644 --- a/tests/v2/run.bats +++ b/tests/v2/run.bats @@ -586,6 +586,34 @@ export BUILDKITE_JOB_ID=1111 unstub buildkite-agent } + +@test "Run with compatibility mode" { + export BUILDKITE_BUILD_NUMBER=1 + export BUILDKITE_COMMAND=pwd + export BUILDKITE_JOB_ID=1111 + export BUILDKITE_PIPELINE_SLUG=test + + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CLEANUP=false + export BUILDKITE_PLUGIN_DOCKER_COMPOSE_COMPATIBILITY=true + + stub docker \ + "compose --compatibility -f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml pull myservice : echo pulled myservice" \ + "compose --compatibility -f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml up -d --scale myservice=0 myservice : echo started dependencies for myservice" \ + "compose --compatibility -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 with use aliases output" + + 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 use aliases output" + unstub docker + unstub buildkite-agent +} + @test "Run with a volumes option" { export BUILDKITE_JOB_ID=1111 export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice