Skip to content

Commit 652a6d7

Browse files
committed
fix merge conflicts
2 parents 2988e17 + e88d05b commit 652a6d7

8 files changed

Lines changed: 104 additions & 12 deletions

File tree

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ The name of the service the command should be run within. If the docker-compose
3333

3434
A list of services to push. You can specify just the service name to push or the format `service:registry:tag` to override where the service's image is pushed to. Needless to say, the image for the service must have been built in the very same step or built and pushed previously to ensure it is available for pushing.
3535

36+
**Important**: when the image for a service is pushed it sets metadata on the build so that future steps will know to use that image to run that service. This can lead to race conditions when pushing multiple images for a service. Can be turned off with the `push-metadata` option.
37+
3638
:warning: If a service does not have an `image` configuration and no registry/tag are specified in the `push` option, pushing of the service will be skipped by docker.
3739

3840
:warning: The `push` command will fail when the image refers to a remote registry that requires a login and the agent has not been authenticated for it (for example, using the [ecr](https://github.com/buildkite-plugins/ecr-buildkite-plugin) or [docker-login](https://github.com/buildkite-plugins/docker-login-buildkite-plugin) plugins).
@@ -171,6 +173,12 @@ Default: `false`
171173

172174
A number of times to retry failed docker pull. Defaults to 0.
173175

176+
#### `push-metadata` (push only, boolean)
177+
178+
Whether to set the metadata aboout the image for a service being pushed.
179+
180+
Default: `true`.
181+
174182
#### `push-retries` (push only, integer)
175183

176184
A number of times to retry failed docker push. Defaults to 0.
@@ -322,11 +330,11 @@ Note that [the effect of this option changes depending on your docker compose CL
322330

323331
#### `entrypoint` (run only)
324332

325-
Sets the `--entrypoint` argument when running `docker compose`.
333+
Sets the `--entrypoint` argument when running `docker compose`, can be set to an empty string.
326334

327335
#### `require-prebuild` (run only, boolean)
328336

329-
If no prebuilt image is found for the run step, it will cause the plugin to fail the step.
337+
Make sure that images for the service being run and all specified in `pull` are found for the run step, fail the step otherwise. Note that specifying a `run-image` will skip this check for the service being run.
330338

331339
The default is `false`.
332340

commands/run.sh

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ for service_name in "${prebuilt_candidates[@]}" ; do
4949
prebuilt_image="$prebuilt_image_override"
5050
elif prebuilt_image=$(get_prebuilt_image "$prebuilt_image_namespace" "$service_name") ; then
5151
echo "~~~ :docker: Found a pre-built image for $service_name"
52+
else
53+
echo "+++ 🚨 No pre-built image found from a previous 'build' step for service ${service_name} and config file."
54+
55+
if [[ "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_REQUIRE_PREBUILD:-}" =~ ^(true|on|1)$ ]]; then
56+
echo "The step specified that it was required"
57+
exit 1
58+
fi
5259
fi
5360

5461
if [[ -n "$prebuilt_image" ]] ; then
@@ -285,7 +292,7 @@ if [[ "$(plugin_read_config RM "true")" == "true" ]]; then
285292
fi
286293

287294
# Optionally sets --entrypoint
288-
if [[ -n "$(plugin_read_config ENTRYPOINT)" ]] ; then
295+
if plugin_config_exists ENTRYPOINT ; then
289296
run_params+=(--entrypoint)
290297
run_params+=("$(plugin_read_config ENTRYPOINT)")
291298
fi
@@ -350,15 +357,6 @@ fi
350357

351358
run_params+=("$run_service")
352359

353-
if [[ ! -f "$override_file" ]] ; then
354-
echo "+++ 🚨 No pre-built image found from a previous 'build' step for this service and config file."
355-
356-
if [[ "${BUILDKITE_PLUGIN_DOCKER_COMPOSE_REQUIRE_PREBUILD:-}" =~ ^(true|on|1)$ ]]; then
357-
echo "The step specified that it was required"
358-
exit 1
359-
fi
360-
fi
361-
362360
up_params+=("up") # this ensures that the array has elements to avoid issues with bash 4.3
363361

364362
if [[ "$(plugin_read_config WAIT "false")" == "true" ]] ; then

docs/examples.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ steps:
2626

2727
The plugin will honor the value of the `COMPOSE_FILE` environment variable if one exists (for example, at the pipeline or step level). But you can also specify custom Docker Compose config files with the `config` option:
2828

29+
2930
```yml
3031
steps:
3132
- command: test.sh

lib/metadata.bash

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ function set_prebuilt_image() {
5656
local service="$2"
5757
local image="$3"
5858

59+
# assumes that the lib/shared.bash has already been sourced
60+
if [ "$(plugin_read_config PUSH_METADATA "true")" != "true" ]; then
61+
plugin_prompt "Not setting metadata for prebuilt image, push-metadata option is set to false"
62+
return 0
63+
fi
64+
5965
plugin_set_metadata "$namespace" "$(prebuilt_image_meta_data_key "$service")" "$image"
6066
}
6167

lib/shared.bash

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ function plugin_read_list_into_result() {
8686
[[ ${#result[@]} -gt 0 ]] || return 1
8787
}
8888

89+
function plugin_config_exists() {
90+
local var="BUILDKITE_PLUGIN_DOCKER_COMPOSE_${1}"
91+
92+
# Check if the variable is set
93+
[ "${!var+is_set}" != "" ]
94+
}
95+
8996
# Returns the name of the docker compose project for this build
9097
function docker_compose_project_name() {
9198
# No dashes or underscores because docker-compose will remove them anyways

plugin.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ configuration:
128128
minimum: 1
129129
pull-retries:
130130
type: integer
131+
push-metadata:
132+
type: boolean
131133
push-retries:
132134
type: integer
133135
quiet-pull:
@@ -211,6 +213,7 @@ configuration:
211213
propagate-uid-gid: [ run ]
212214
pull: [ run ]
213215
pull-retries: [ run ]
216+
push-metadata: [ push ]
214217
push-retries: [ push ]
215218
quiet-pull: [ push, run]
216219
quiet-push: [ push ]

tests/metadata.bats

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,28 @@ load '../lib/metadata'
5353
assert_output --partial "buildkite-agent meta-data get docker-compose-plugin-built-image-tag-test"
5454
unstub buildkite-agent
5555
}
56+
57+
@test "Set prebuilt image in metadata" {
58+
stub buildkite-agent \
59+
"meta-data set docker-compose-plugin-built-image-tag-test \* : echo setting metadata to \$4"
60+
61+
run set_prebuilt_image "docker-compose-plugin-" "test" "new-image"
62+
63+
assert_success
64+
65+
assert_output --partial "buildkite-agent meta-data set docker-compose-plugin-built-image-tag-test new-image"
66+
67+
unstub buildkite-agent
68+
}
69+
70+
@test "Can skip setting prebuilt image in metadata" {
71+
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_PUSH_METADATA=false
72+
73+
run set_prebuilt_image "docker-compose-plugin-" "test" "new-image"
74+
75+
assert_success
76+
77+
refute_output --partial "buildkite-agent meta-data set docker-compose-plugin-built-image-tag-test new-image"
78+
assert_output --partial "Not setting metadata for prebuilt image, push-metadata option is set to false"
79+
}
80+

tests/run.bats

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,27 @@ teardown() {
5959
unstub buildkite-agent
6060
}
6161

62+
@test "Fail running without a prebuilt image for pull service and require-prebuild" {
63+
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice
64+
export BUILDKITE_COMMAND="echo hello world"
65+
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_REQUIRE_PREBUILD=true
66+
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_PULL=other-service
67+
68+
stub buildkite-agent \
69+
"meta-data exists docker-compose-plugin-built-image-tag-myservice : exit 0" \
70+
"meta-data get docker-compose-plugin-built-image-tag-myservice : echo myservice-image" \
71+
"meta-data exists docker-compose-plugin-built-image-tag-other-service : exit 1"
72+
73+
run "$PWD"/hooks/command
74+
75+
assert_failure
76+
77+
assert_output --partial "Found a pre-built image for myservice"
78+
assert_output --partial "No pre-built image found"
79+
80+
unstub buildkite-agent
81+
}
82+
6283
@test "Run without a prebuilt image and an empty command" {
6384
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice
6485
export BUILDKITE_COMMAND=""
@@ -900,6 +921,29 @@ cmd3"
900921
unstub buildkite-agent
901922
}
902923

924+
@test "Run with empty entrypoint" {
925+
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice
926+
export BUILDKITE_COMMAND=""
927+
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CHECK_LINKED_CONTAINERS=false
928+
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_CLEANUP=false
929+
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_ENTRYPOINT=""
930+
931+
stub docker \
932+
"compose -f docker-compose.yml -p buildkite1111 up -d --scale myservice=0 myservice : echo ran myservice dependencies" \
933+
"compose -f docker-compose.yml -p buildkite1111 run --name buildkite1111_myservice_build_1 -T --rm --entrypoint '' myservice : echo ran myservice"
934+
935+
stub buildkite-agent \
936+
"meta-data exists docker-compose-plugin-built-image-tag-myservice : exit 1"
937+
938+
run "$PWD"/hooks/command
939+
940+
assert_success
941+
assert_output --partial "ran myservice"
942+
943+
unstub docker
944+
unstub buildkite-agent
945+
}
946+
903947
@test "Run with mount-buildkite-agent enabled" {
904948
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice
905949
export BUILDKITE_COMMAND=""

0 commit comments

Comments
 (0)