Skip to content

Commit 32c193e

Browse files
committed
address feedback
Signed-off-by: Jared Quick <jared.quick@salesforce.com>
1 parent 862bde3 commit 32c193e

5 files changed

Lines changed: 48 additions & 20 deletions

File tree

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ Important: this only works when building a single service, an error will be gene
6464

6565
A list of KEY=VALUE that are passed through as build arguments when image is being built.
6666

67-
#### `labels` (build and run only, string or array)
68-
69-
A list of KEY=VALUE that are passed through as service labels when image is being built or ran. These will be merged with any service labels defined in the compose file.
70-
7167
#### `env` or `environment` (run only, string or array)
7268

7369
A list of either KEY or KEY=VALUE that are passed through as environment variables to the container.
@@ -272,6 +268,14 @@ If set to true, adds useful Docker labels to the primary container. See [Contain
272268

273269
The default is `true`.
274270

271+
#### `labels` (run only, string or array)
272+
273+
A list of KEY=VALUE that are passed through as service labels when image is being ran. These will be merged with any service labels defined in the compose file.
274+
275+
#### `build-labels` (build only, string or array)
276+
277+
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.
278+
275279
#### `compatibility` (boolean)
276280

277281
If set to true, all docker compose commands will rum with compatibility mode. Equivalent to `--compatibility` in docker compose.

commands/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ for service_name in $(plugin_read_list BUILD) ; do
5050
labels=()
5151
while read -r label ; do
5252
[[ -n "${label:-}" ]] && labels+=("${label}")
53-
done <<< "$(plugin_read_list LABELS)"
53+
done <<< "$(plugin_read_list BUILD_LABELS)"
5454

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

lib/shared.bash

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -167,26 +167,24 @@ function build_image_override_file_with_version() {
167167
shift 3
168168

169169
# load cache_from array
170-
cache_from=()
171-
if (( $# > 0 )); then
172-
cache_from_amt=$1
173-
shift
174-
while (( cache_from_amt-- > 0 )); do
170+
cache_from_amt="${1:-0}"
171+
[[ -n "${1:-}" ]] && shift; # remove the value if not empty
172+
if [[ "${cache_from_amt}" -gt 0 ]]; then
173+
for amt in $(seq 1 "$cache_from_amt"); do
175174
cache_from+=( "$1" ); shift
176175
done
177176
fi
178177

179178
# load labels array
180-
labels=()
181-
if (( $# > 0 )); then
182-
labels_amt=$1
183-
shift
184-
while (( labels_amt-- > 0 )); do
179+
labels_amt="${1:-0}"
180+
[[ -n "${1:-}" ]] && shift; # remove the value if not empty
181+
if [[ "${labels_amt}" -gt 0 ]]; then
182+
for amt in $(seq 1 "$labels_amt"); do
185183
labels+=( "$1" ); shift
186184
done
187185
fi
188186

189-
if [[ -z "$image_name" ]] && [[ -z "$target" ]] && [[ "${#cache_from[@]}" -eq 0 ]] && [[ "${#labels[@]}" -eq 0 ]]; then
187+
if [[ -z "$image_name" ]] && [[ -z "$target" ]] && [[ "$cache_from_amt" -eq 0 ]] && [[ "$labels_amt" -eq 0 ]]; then
190188
# should not print out an empty service
191189
continue
192190
fi
@@ -197,15 +195,15 @@ function build_image_override_file_with_version() {
197195
printf " image: %s\\n" "$image_name"
198196
fi
199197

200-
if [[ "${#cache_from[@]}" -gt 0 ]] || [[ -n "$target" ]] || [[ "${#labels[@]}" -gt 0 ]]; then
198+
if [[ "$cache_from_amt" -gt 0 ]] || [[ -n "$target" ]] || [[ "$labels_amt" -gt 0 ]]; then
201199
printf " build:\\n"
202200
fi
203201

204202
if [[ -n "$target" ]]; then
205203
printf " target: %s\\n" "$target"
206204
fi
207205

208-
if [[ "${#cache_from[@]}" -gt 0 ]] ; then
206+
if [[ "$cache_from_amt" -gt 0 ]] ; then
209207
if ! docker_compose_supports_cache_from "$version" ; then
210208
echo "Unsupported Docker Compose config file version: $version"
211209
echo "The 'cache_from' option can only be used with Compose file versions 2.2 or 3.2 and above."
@@ -220,7 +218,7 @@ function build_image_override_file_with_version() {
220218
done
221219
fi
222220

223-
if [[ "${#labels[@]}" -gt 0 ]] ; then
221+
if [[ "$labels_amt" -gt 0 ]] ; then
224222
printf " labels:\\n"
225223
for label in "${labels[@]}"; do
226224
printf " - %s\\n" "${label}"

plugin.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ configuration:
2323
build-alias:
2424
type: [ string, array ]
2525
minimum: 1
26+
build-labels:
27+
type: [ string, array ]
28+
minimum: 1
2629
build-parallel:
2730
type: boolean
2831
buildkit:
@@ -141,6 +144,7 @@ configuration:
141144
ansi: [ run ]
142145
args: [ build ]
143146
build-alias: [ push ]
147+
build-labels: [ build ]
144148
build-parallel: [ build ]
145149
buildkit: [ build ]
146150
buildkit-inline-cache: [ build ]
@@ -153,7 +157,7 @@ configuration:
153157
environment: [ run ]
154158
expand-volume-vars: [ volumes ]
155159
graceful-shutdown: [ run ]
156-
labels: [ build, run ]
160+
labels: [ run ]
157161
leave-volumes: [ run ]
158162
mount-buildkite-agent: [ run ]
159163
mount-checkout: [ run ]

tests/image-override-file.bats

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,28 @@ EOF
132132
assert_output "$myservice_override_file3"
133133
}
134134

135+
@test "Build a docker-compose file with multiple cache-from and multiple labels and target" {
136+
myservice_override_file3=$(cat <<-EOF
137+
version: '3.2'
138+
services:
139+
myservice:
140+
image: newimage:1.0.0
141+
build:
142+
target: build
143+
cache_from:
144+
- my.repository/myservice:latest
145+
- my.repository/myservice:target
146+
labels:
147+
- com.buildkite.test=test
148+
- com.buildkite.test2=test2
149+
EOF
150+
)
151+
152+
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"
153+
154+
assert_success
155+
assert_output "$myservice_override_file3"
156+
}
135157

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

0 commit comments

Comments
 (0)