Skip to content

Commit b83f97f

Browse files
authored
Merge pull request #429 from jquick/jq/add_build_labels
Add service label build options
2 parents 05ddf7d + 7d923ea commit b83f97f

6 files changed

Lines changed: 158 additions & 18 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ If set to `true` it will mount onto `/workdir`, unless `workdir` is set, in whic
146146

147147
Default: `false`
148148

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

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

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

269269
The default is `true`.
270270

271+
#### `build-labels` (build only, string or array)
272+
273+
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.
274+
271275
#### `compatibility` (boolean)
272276

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

commands/build.sh

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,27 @@ for service_name in $(plugin_read_list BUILD) ; do
4343
image_name="" # no longer used here
4444

4545
cache_from=()
46-
cache_length=0
47-
4846
for cache_line in $(get_caches_for_service "$service_name"); do
4947
cache_from+=("$cache_line")
50-
cache_length=$((cache_length + 1))
5148
done
5249

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

56-
if [[ "${cache_length:-0}" -gt 0 ]]; then
55+
if [[ -n "${target}" ]] || [[ "${#labels[@]}" -gt 0 ]] || [[ "${#cache_from[@]}" -gt 0 ]]; then
56+
build_images+=("$service_name" "${image_name}" "${target}")
57+
58+
build_images+=("${#cache_from[@]}")
59+
if [[ "${#cache_from[@]}" -gt 0 ]]; then
5760
build_images+=("${cache_from[@]}")
5861
fi
62+
63+
build_images+=("${#labels[@]}")
64+
if [[ "${#labels[@]}" -gt 0 ]]; then
65+
build_images+=("${labels[@]}")
66+
fi
5967
fi
6068
done
6169

commands/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ prebuilt_services=()
4343
for service_name in "${prebuilt_candidates[@]}" ; do
4444
if prebuilt_image=$(get_prebuilt_image "$service_name") ; then
4545
echo "~~~ :docker: Found a pre-built image for $service_name"
46-
prebuilt_service_overrides+=("$service_name" "$prebuilt_image" "" 0)
46+
prebuilt_service_overrides+=("$service_name" "$prebuilt_image" "" 0 0)
4747
prebuilt_services+=("$service_name")
4848

4949
# If it's prebuilt, we need to pull it down

lib/shared.bash

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,29 @@ function build_image_override_file_with_version() {
164164
service_name=$1
165165
image_name=$2
166166
target=$3
167-
cache_from_amt=$4
168-
shift 4
167+
shift 3
168+
169+
# load cache_from array
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+
cache_from=()
174+
for _ in $(seq 1 "$cache_from_amt"); do
175+
cache_from+=( "$1" ); shift
176+
done
177+
fi
178+
179+
# load labels array
180+
labels_amt="${1:-0}"
181+
[[ -n "${1:-}" ]] && shift; # remove the value if not empty
182+
if [[ "${labels_amt}" -gt 0 ]]; then
183+
labels=()
184+
for _ in $(seq 1 "$labels_amt"); do
185+
labels+=( "$1" ); shift
186+
done
187+
fi
169188

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

181-
if [[ "$cache_from_amt" -gt 0 ]] || [[ -n "$target" ]]; then
200+
if [[ "$cache_from_amt" -gt 0 ]] || [[ -n "$target" ]] || [[ "$labels_amt" -gt 0 ]]; then
182201
printf " build:\\n"
183202
fi
184203

@@ -196,10 +215,16 @@ function build_image_override_file_with_version() {
196215
fi
197216

198217
printf " cache_from:\\n"
199-
for cache_from_i in $(seq 1 "$cache_from_amt"); do
200-
printf " - %s\\n" "${!cache_from_i}"
218+
for cache_from_i in "${cache_from[@]}"; do
219+
printf " - %s\\n" "${cache_from_i}"
220+
done
221+
fi
222+
223+
if [[ "$labels_amt" -gt 0 ]] ; then
224+
printf " labels:\\n"
225+
for label in "${labels[@]}"; do
226+
printf " - %s\\n" "${label}"
201227
done
202-
shift "$cache_from_amt"
203228
fi
204229
done
205230
}
@@ -277,4 +302,4 @@ function validate_tag {
277302
else
278303
return 1
279304
fi
280-
}
305+
}

plugin.yml

Lines changed: 4 additions & 0 deletions
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:
@@ -138,6 +141,7 @@ configuration:
138141
ansi: [ run ]
139142
args: [ build ]
140143
build-alias: [ push ]
144+
build-labels: [ build ]
141145
build-parallel: [ build ]
142146
buildkit: [ build ]
143147
buildkit-inline-cache: [ build ]

tests/image-override-file.bats

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,30 @@ EOF
2929
)
3030

3131
run build_image_override_file_with_version "2.1" \
32-
"myservice1" "newimage1:1.0.0" "" 0 \
33-
"myservice2" "newimage2:1.0.0" "" 0
32+
"myservice1" "newimage1:1.0.0" "" 0 0 \
33+
"myservice2" "newimage2:1.0.0" "" 0 0
3434

3535
assert_success
3636
assert_output "$myservice_override_file2"
3737
}
3838

39+
@test "Build a docker-compose file with target" {
40+
myservice_override_file3=$(cat <<-EOF
41+
version: '3.2'
42+
services:
43+
myservice:
44+
image: newimage:1.0.0
45+
build:
46+
target: build
47+
EOF
48+
)
49+
50+
run build_image_override_file_with_version "3.2" "myservice" "newimage:1.0.0" "build" 0
51+
52+
assert_success
53+
assert_output "$myservice_override_file3"
54+
}
55+
3956
@test "Build a docker-compose file with cache-from" {
4057
myservice_override_file3=$(cat <<-EOF
4158
version: '3.2'
@@ -73,6 +90,88 @@ EOF
7390
assert_output "$myservice_override_file4"
7491
}
7592

93+
@test "Build a docker-compose file with labels" {
94+
myservice_override_file3=$(cat <<-EOF
95+
version: '3.2'
96+
services:
97+
myservice:
98+
image: newimage:1.0.0
99+
build:
100+
labels:
101+
- com.buildkite.test=test
102+
EOF
103+
)
104+
105+
run build_image_override_file_with_version "3.2" "myservice" "newimage:1.0.0" "" 0 1 "com.buildkite.test=test"
106+
107+
assert_success
108+
assert_output "$myservice_override_file3"
109+
}
110+
111+
@test "Build a docker-compose file with multiple labels" {
112+
myservice_override_file3=$(cat <<-EOF
113+
version: '3.2'
114+
services:
115+
myservice:
116+
image: newimage:1.0.0
117+
build:
118+
labels:
119+
- com.buildkite.test=test
120+
- com.buildkite.test2=test2
121+
EOF
122+
)
123+
124+
run build_image_override_file_with_version "3.2" "myservice" "newimage:1.0.0" "" 0 2 "com.buildkite.test=test" "com.buildkite.test2=test2"
125+
126+
assert_success
127+
assert_output "$myservice_override_file3"
128+
}
129+
130+
@test "Build a docker-compose file with multiple cache-from and multiple labels" {
131+
myservice_override_file3=$(cat <<-EOF
132+
version: '3.2'
133+
services:
134+
myservice:
135+
image: newimage:1.0.0
136+
build:
137+
cache_from:
138+
- my.repository/myservice:latest
139+
- my.repository/myservice:target
140+
labels:
141+
- com.buildkite.test=test
142+
- com.buildkite.test2=test2
143+
EOF
144+
)
145+
146+
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"
147+
148+
assert_success
149+
assert_output "$myservice_override_file3"
150+
}
151+
152+
@test "Build a docker-compose file with multiple cache-from and multiple labels and target" {
153+
myservice_override_file3=$(cat <<-EOF
154+
version: '3.2'
155+
services:
156+
myservice:
157+
image: newimage:1.0.0
158+
build:
159+
target: build
160+
cache_from:
161+
- my.repository/myservice:latest
162+
- my.repository/myservice:target
163+
labels:
164+
- com.buildkite.test=test
165+
- com.buildkite.test2=test2
166+
EOF
167+
)
168+
169+
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"
170+
171+
assert_success
172+
assert_output "$myservice_override_file3"
173+
}
174+
76175
@test "Build a docker-compose file with cache-from and compose-file version 2" {
77176
run build_image_override_file_with_version "2" "myservice" "newimage:1.0.0" "" 1 "my.repository/myservice:latest"
78177

0 commit comments

Comments
 (0)