Skip to content

Commit

Permalink
Merge pull request #432 from donbobka/add-metadata-key-prefix
Browse files Browse the repository at this point in the history
Add metadata key prefix to support builds with independent clusters/registries
  • Loading branch information
pzeballos authored Aug 23, 2024
2 parents 9122f6a + 61b3c2b commit a71635d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 19 deletions.
12 changes: 7 additions & 5 deletions commands/push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ if plugin_read_list_into_result BUILDKITE_PLUGIN_DOCKER_COMPOSE_BUILD; then
build_services=("${result[@]}")
fi

prebuilt_image_namespace="$(plugin_read_config PREBUILT_IMAGE_NAMESPACE 'docker-compose-plugin-')"

# Then we figure out what to push, and where
for line in $(plugin_read_list PUSH) ; do
IFS=':' read -r -a tokens <<< "$line"
Expand All @@ -37,7 +39,7 @@ for line in $(plugin_read_list PUSH) ; do
elif in_array "${service_name}" "${build_services[@]}"; then
echo "~~~ :docker: Service was built in this step, using that image"
service_image="$(default_compose_image_for_service "${service_name}")"
elif prebuilt_image="$(get_prebuilt_image "$service_name")"; then
elif prebuilt_image="$(get_prebuilt_image "$prebuilt_image_namespace" "$service_name")"; then
echo "~~~ :docker: Using pre-built image ${prebuilt_image}"

# Only pull it down once
Expand All @@ -63,15 +65,15 @@ for line in $(plugin_read_list PUSH) ; do
if [[ ${#tokens[@]} -eq 1 ]] ; then
echo "${group_type} :docker: Pushing images for ${service_name}" >&2;
retry "$push_retries" run_docker_compose push "${service_name}"
set_prebuilt_image "${service_name}" "${service_image}"
set_prebuilt_image "${prebuilt_image_namespace}" "${service_name}" "${service_image}"
target_image="${service_image}" # necessary for build-alias
# push: "service-name:repo:tag"
else
target_image="$(IFS=:; echo "${tokens[*]:1}")"
echo "${group_type} :docker: Pushing image $target_image" >&2;
plugin_prompt_and_run docker tag "$service_image" "$target_image"
retry "$push_retries" plugin_prompt_and_run docker push "$target_image"
set_prebuilt_image "${service_name}" "${target_image}"
set_prebuilt_image "${prebuilt_image_namespace}" "${service_name}" "${target_image}"
fi
done

Expand All @@ -82,5 +84,5 @@ for service_alias in $(plugin_read_list BUILD_ALIAS) ; do
exit 1
fi

set_prebuilt_image "$service_alias" "${target_image}"
done
set_prebuilt_image "${prebuilt_image_namespace}" "$service_alias" "${target_image}"
done
4 changes: 3 additions & 1 deletion commands/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ while read -r name ; do
fi
done <<< "$(plugin_read_list PULL)"

prebuilt_image_namespace="$(plugin_read_config PREBUILT_IMAGE_NAMESPACE 'docker-compose-plugin-')"

# A list of tuples of [service image cache_from] for build_image_override_file
prebuilt_service_overrides=()
prebuilt_services=()
Expand All @@ -45,7 +47,7 @@ for service_name in "${prebuilt_candidates[@]}" ; do
if [[ -n "$prebuilt_image_override" ]] && [[ "$service_name" == "$run_service" ]] ; then
echo "~~~ :docker: Overriding run image for $service_name"
prebuilt_image="$prebuilt_image_override"
elif prebuilt_image=$(get_prebuilt_image "$service_name") ; then
elif prebuilt_image=$(get_prebuilt_image "$prebuilt_image_namespace" "$service_name") ; then
echo "~~~ :docker: Found a pre-built image for $service_name"
fi

Expand Down
28 changes: 20 additions & 8 deletions lib/metadata.bash
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ function plugin_check_metadata_exists() {

# Read agent metadata for the plugin
function plugin_get_metadata() {
local key="docker-compose-plugin-$1"
local namespace="$1"
local key="$2"

key="$namespace$key"

if plugin_check_metadata_exists "$key"; then
plugin_prompt buildkite-agent meta-data get "$key"
buildkite-agent meta-data get "$key"
Expand All @@ -18,8 +22,12 @@ function plugin_get_metadata() {

# Write agent metadata for the plugin
function plugin_set_metadata() {
local key="docker-compose-plugin-$1"
local value="$2"
local namespace="$1"
local key="$2"
local value="$3"

key="$namespace$key"

plugin_prompt_and_must_run buildkite-agent meta-data set "$key" "$value"
}

Expand All @@ -44,13 +52,17 @@ function prebuilt_image_meta_data_key() {

# Sets a prebuilt image for a service name
function set_prebuilt_image() {
local service="$1"
local image="$2"
plugin_set_metadata "$(prebuilt_image_meta_data_key "$service")" "$image"
local namespace="$1"
local service="$2"
local image="$3"

plugin_set_metadata "$namespace" "$(prebuilt_image_meta_data_key "$service")" "$image"
}

# Gets a prebuilt image for a service name
function get_prebuilt_image() {
local service="$1"
plugin_get_metadata "$(prebuilt_image_meta_data_key "$service")"
local namespace="$1"
local service="$2"

plugin_get_metadata "$namespace" "$(prebuilt_image_meta_data_key "$service")"
}
2 changes: 2 additions & 0 deletions plugin.yml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ configuration:
type: boolean
pre-run-dependencies:
type: boolean
prebuilt-image-namespace:
type: string
propagate-environment:
type: boolean
propagate-uid-gid:
Expand Down
10 changes: 5 additions & 5 deletions tests/metadata.bats
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ load '../lib/metadata'
# Only expect the 'exists' command to be called, not the 'get'
stub buildkite-agent "meta-data exists docker-compose-plugin-built-image-tag-test : exit 1"

run get_prebuilt_image "test"
run get_prebuilt_image "docker-compose-plugin-" "test"

assert_failure
unstub buildkite-agent
}

@test "Only get prebuilt image from metadata if 'exists' check returns true" {
stub buildkite-agent \
"meta-data exists docker-compose-plugin-built-image-tag-test : exit 0" \
"meta-data get docker-compose-plugin-built-image-tag-test : exit 0"
"meta-data get docker-compose-plugin-built-image-tag-test : exit 0"

run get_prebuilt_image "docker-compose-plugin-" "test"

run get_prebuilt_image "test"

assert_success
assert_output --partial "buildkite-agent meta-data get docker-compose-plugin-built-image-tag-test"
unstub buildkite-agent
Expand Down

0 comments on commit a71635d

Please sign in to comment.