Skip to content

Commit

Permalink
Merge pull request #429 from jquick/jq/add_build_labels
Browse files Browse the repository at this point in the history
Add service label build options
  • Loading branch information
pzeballos authored Feb 28, 2024
2 parents 05ddf7d + 7d923ea commit b83f97f
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 18 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ If set to `true` it will mount onto `/workdir`, unless `workdir` is set, in whic

Default: `false`

### `buildkit-inline-cache` (optional, build-only, boolean)
#### `buildkit-inline-cache` (optional, build-only, boolean)

Whether to pass the `BUILDKIT_INLINE_CACHE=1` build arg when building an image. Can be safely used in combination with `args`.

Expand Down Expand Up @@ -268,6 +268,10 @@ If set to true, adds useful Docker labels to the primary container. See [Contain

The default is `true`.

#### `build-labels` (build only, string or array)

A list of KEY=VALUE that are passed through as service labels when image is being built. These will be merged with any service labels defined in the compose file.

#### `compatibility` (boolean)

If set to true, all docker compose commands will rum with compatibility mode. Equivalent to `--compatibility` in docker compose.
Expand Down
20 changes: 14 additions & 6 deletions commands/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,27 @@ for service_name in $(plugin_read_list BUILD) ; do
image_name="" # no longer used here

cache_from=()
cache_length=0

for cache_line in $(get_caches_for_service "$service_name"); do
cache_from+=("$cache_line")
cache_length=$((cache_length + 1))
done

if [[ -n "${target}" ]] || [[ "${cache_length:-0}" -gt 0 ]]; then
build_images+=("$service_name" "${image_name}" "${target}" "${cache_length}")
labels=()
while read -r label ; do
[[ -n "${label:-}" ]] && labels+=("${label}")
done <<< "$(plugin_read_list BUILD_LABELS)"

if [[ "${cache_length:-0}" -gt 0 ]]; then
if [[ -n "${target}" ]] || [[ "${#labels[@]}" -gt 0 ]] || [[ "${#cache_from[@]}" -gt 0 ]]; then
build_images+=("$service_name" "${image_name}" "${target}")

build_images+=("${#cache_from[@]}")
if [[ "${#cache_from[@]}" -gt 0 ]]; then
build_images+=("${cache_from[@]}")
fi

build_images+=("${#labels[@]}")
if [[ "${#labels[@]}" -gt 0 ]]; then
build_images+=("${labels[@]}")
fi
fi
done

Expand Down
2 changes: 1 addition & 1 deletion commands/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ prebuilt_services=()
for service_name in "${prebuilt_candidates[@]}" ; do
if prebuilt_image=$(get_prebuilt_image "$service_name") ; then
echo "~~~ :docker: Found a pre-built image for $service_name"
prebuilt_service_overrides+=("$service_name" "$prebuilt_image" "" 0)
prebuilt_service_overrides+=("$service_name" "$prebuilt_image" "" 0 0)
prebuilt_services+=("$service_name")

# If it's prebuilt, we need to pull it down
Expand Down
41 changes: 33 additions & 8 deletions lib/shared.bash
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,29 @@ function build_image_override_file_with_version() {
service_name=$1
image_name=$2
target=$3
cache_from_amt=$4
shift 4
shift 3

# load cache_from array
cache_from_amt="${1:-0}"
[[ -n "${1:-}" ]] && shift; # remove the value if not empty
if [[ "${cache_from_amt}" -gt 0 ]]; then
cache_from=()
for _ in $(seq 1 "$cache_from_amt"); do
cache_from+=( "$1" ); shift
done
fi

# load labels array
labels_amt="${1:-0}"
[[ -n "${1:-}" ]] && shift; # remove the value if not empty
if [[ "${labels_amt}" -gt 0 ]]; then
labels=()
for _ in $(seq 1 "$labels_amt"); do
labels+=( "$1" ); shift
done
fi

if [[ -z "$image_name" ]] && [[ -z "$target" ]] && [[ "$cache_from_amt" -eq 0 ]]; then
if [[ -z "$image_name" ]] && [[ -z "$target" ]] && [[ "$cache_from_amt" -eq 0 ]] && [[ "$labels_amt" -eq 0 ]]; then
# should not print out an empty service
continue
fi
Expand All @@ -178,7 +197,7 @@ function build_image_override_file_with_version() {
printf " image: %s\\n" "$image_name"
fi

if [[ "$cache_from_amt" -gt 0 ]] || [[ -n "$target" ]]; then
if [[ "$cache_from_amt" -gt 0 ]] || [[ -n "$target" ]] || [[ "$labels_amt" -gt 0 ]]; then
printf " build:\\n"
fi

Expand All @@ -196,10 +215,16 @@ function build_image_override_file_with_version() {
fi

printf " cache_from:\\n"
for cache_from_i in $(seq 1 "$cache_from_amt"); do
printf " - %s\\n" "${!cache_from_i}"
for cache_from_i in "${cache_from[@]}"; do
printf " - %s\\n" "${cache_from_i}"
done
fi

if [[ "$labels_amt" -gt 0 ]] ; then
printf " labels:\\n"
for label in "${labels[@]}"; do
printf " - %s\\n" "${label}"
done
shift "$cache_from_amt"
fi
done
}
Expand Down Expand Up @@ -277,4 +302,4 @@ function validate_tag {
else
return 1
fi
}
}
4 changes: 4 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ configuration:
build-alias:
type: [ string, array ]
minimum: 1
build-labels:
type: [ string, array ]
minimum: 1
build-parallel:
type: boolean
buildkit:
Expand Down Expand Up @@ -138,6 +141,7 @@ configuration:
ansi: [ run ]
args: [ build ]
build-alias: [ push ]
build-labels: [ build ]
build-parallel: [ build ]
buildkit: [ build ]
buildkit-inline-cache: [ build ]
Expand Down
103 changes: 101 additions & 2 deletions tests/image-override-file.bats
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,30 @@ EOF
)

run build_image_override_file_with_version "2.1" \
"myservice1" "newimage1:1.0.0" "" 0 \
"myservice2" "newimage2:1.0.0" "" 0
"myservice1" "newimage1:1.0.0" "" 0 0 \
"myservice2" "newimage2:1.0.0" "" 0 0

assert_success
assert_output "$myservice_override_file2"
}

@test "Build a docker-compose file with target" {
myservice_override_file3=$(cat <<-EOF
version: '3.2'
services:
myservice:
image: newimage:1.0.0
build:
target: build
EOF
)

run build_image_override_file_with_version "3.2" "myservice" "newimage:1.0.0" "build" 0

assert_success
assert_output "$myservice_override_file3"
}

@test "Build a docker-compose file with cache-from" {
myservice_override_file3=$(cat <<-EOF
version: '3.2'
Expand Down Expand Up @@ -73,6 +90,88 @@ EOF
assert_output "$myservice_override_file4"
}

@test "Build a docker-compose file with labels" {
myservice_override_file3=$(cat <<-EOF
version: '3.2'
services:
myservice:
image: newimage:1.0.0
build:
labels:
- com.buildkite.test=test
EOF
)

run build_image_override_file_with_version "3.2" "myservice" "newimage:1.0.0" "" 0 1 "com.buildkite.test=test"

assert_success
assert_output "$myservice_override_file3"
}

@test "Build a docker-compose file with multiple labels" {
myservice_override_file3=$(cat <<-EOF
version: '3.2'
services:
myservice:
image: newimage:1.0.0
build:
labels:
- com.buildkite.test=test
- com.buildkite.test2=test2
EOF
)

run build_image_override_file_with_version "3.2" "myservice" "newimage:1.0.0" "" 0 2 "com.buildkite.test=test" "com.buildkite.test2=test2"

assert_success
assert_output "$myservice_override_file3"
}

@test "Build a docker-compose file with multiple cache-from and multiple labels" {
myservice_override_file3=$(cat <<-EOF
version: '3.2'
services:
myservice:
image: newimage:1.0.0
build:
cache_from:
- my.repository/myservice:latest
- my.repository/myservice:target
labels:
- com.buildkite.test=test
- com.buildkite.test2=test2
EOF
)

run build_image_override_file_with_version "3.2" "myservice" "newimage:1.0.0" "" 2 "my.repository/myservice:latest" "my.repository/myservice:target" 2 "com.buildkite.test=test" "com.buildkite.test2=test2"

assert_success
assert_output "$myservice_override_file3"
}

@test "Build a docker-compose file with multiple cache-from and multiple labels and target" {
myservice_override_file3=$(cat <<-EOF
version: '3.2'
services:
myservice:
image: newimage:1.0.0
build:
target: build
cache_from:
- my.repository/myservice:latest
- my.repository/myservice:target
labels:
- com.buildkite.test=test
- com.buildkite.test2=test2
EOF
)

run build_image_override_file_with_version "3.2" "myservice" "newimage:1.0.0" "build" 2 "my.repository/myservice:latest" "my.repository/myservice:target" 2 "com.buildkite.test=test" "com.buildkite.test2=test2"

assert_success
assert_output "$myservice_override_file3"
}

@test "Build a docker-compose file with cache-from and compose-file version 2" {
run build_image_override_file_with_version "2" "myservice" "newimage:1.0.0" "" 1 "my.repository/myservice:latest"

Expand Down

0 comments on commit b83f97f

Please sign in to comment.