Skip to content

Commit 94c3192

Browse files
feat(packages/scripts): add multi-arch image collection and push support (#702)
Let's make it can work standalone without event driven. - In pushing to google cloud artifacts repo, it's too complex and expensive to introduce the Pub/Sub stack. - After this refactoring, it can work in IDC Clusters or any other cloud clusters. - Previously, we compute the multi-arch tags in another task/scripts, it make the logic not atomically with the `packages.yaml.tmpl` file, it will solve it. --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent a5750d9 commit 94c3192

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

packages/scripts/build-package-images.sh.tmpl

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ parse_arguments() {
3737
NEED_BUILD_BIN="false" # Default value is false
3838
NEED_PUSH_IMAGE="true" # Default value is true
3939
NEED_TAG_MORE="false" # Default value is false
40+
NEED_COLLECT_MULT_ARCH="false" # Default value is false
4041
KANIKO_EXECUTOR="/kaniko/executor"
4142
PUSH_RESULT_SAVE_FILE="result-images.yaml"
4243

@@ -62,6 +63,10 @@ parse_arguments() {
6263
NEED_TAG_MORE="true"
6364
shift
6465
;;
66+
-m)
67+
NEED_COLLECT_MULT_ARCH="true"
68+
shift
69+
;;
6570
-k)
6671
KANIKO_EXECUTOR="$2"
6772
shift 2
@@ -91,6 +96,7 @@ print_help() {
9196
echo " -p Enable build and push images (default true)"
9297
echo " -P Skip build and push images (default true)"
9398
echo " -t Enable add more tags, else just pushed for first tag (default false)"
99+
echo " -m Enable collect and push multi-arch images (default false)"
94100
echo " -k kaniko_executor Set the Kaniko executor path (default: '/kaniko/executor')"
95101
echo " -h Print this help message"
96102
}
@@ -217,14 +223,20 @@ build_and_push_images() {
217223
cat "$digests_file"
218224
}
219225

220-
{{- $tag_suffix := printf "_%s_%s" .os .arch }}
226+
{{- $tag_suffix := "" }}
221227
{{- if ne .profile "release" }}
222-
{{- $tag_suffix = printf "-%s_%s_%s" .profile .os .arch }}
228+
{{- $tag_suffix = printf "-%s" .profile }}
223229
{{- end }}
230+
{{- $platform_tag_suffix := printf "%s_%s_%s" $tag_suffix .os .arch }}
224231

225-
{{- $tags := coll.Slice }}
232+
{{- $base_tags := coll.Slice }}
226233
{{- range $index, $tag := .artifactory.tags }}
227-
{{- $tags = append (printf "%s%s" $tag $tag_suffix) $tags }}
234+
{{- $base_tags = append (printf "%s%s" $tag $tag_suffix) $base_tags }}
235+
{{- end }}
236+
237+
{{- $tags := coll.Slice }}
238+
{{- range $index, $tag := $base_tags }}
239+
{{- $tags = append (printf "%s%s" $tag $platform_tag_suffix) $tags }}
228240
{{- end }}
229241

230242
# Write results to file.
@@ -255,4 +267,26 @@ add_more_tags() {
255267
{{- end }}
256268
}
257269

270+
collect_and_push_multi_arch_images() {
271+
{{- $self_tag := index $tags 0 }}
272+
{{- $other_arch := ternary "arm64" "amd64" (eq .arch "amd64") }}
273+
{{- $other_tag := printf "%s_%s_%s" (index $base_tags 0) .os $other_arch }}
274+
275+
{{- range $index, $image := (.artifacts | jq `map(select(.type == "image" and .if != false))`) }}
276+
277+
# 📦 try to collect and push multi-arch image: {{ $image.artifactory.repo }}:{{ index $base_tags 0 }}
278+
{{- range $i, $tag := $base_tags }}
279+
{{- if eq $i 0 }}
280+
if ! crane digest {{ $image.artifactory.repo }}:{{ $other_tag }}; then
281+
echo "🤷 Image {{ $image.artifactory.repo }}:{{ $other_tag }} not found."
282+
exit 0
283+
fi
284+
crane index append -m {{ $image.artifactory.repo }}:{{ $self_tag }} -m {{ $image.artifactory.repo }}:{{ $other_tag }} -t {{ $image.artifactory.repo }}:{{ $tag }}
285+
{{- else }}
286+
crane tag {{ $image.artifactory.repo }}:{{ index $base_tags 0 }} {{ $tag }}
287+
{{- end }}
288+
{{- end }}
289+
{{- end }}
290+
}
291+
258292
main "$@"

0 commit comments

Comments
 (0)