From 2209355707095b734aad9039aff4c21c403e5d11 Mon Sep 17 00:00:00 2001 From: James Tumber Date: Tue, 10 Jan 2023 13:11:49 +0000 Subject: [PATCH] Use buildx for cross-platform builds Use Docker buildx build tooling to create multi-arch manifest instead of manually adding images. Test build: https://hub.docker.com/r/jamesnelson/metrics-scraper/tags Signed-off-by: James Tumber --- Dockerfile | 10 +++++++--- hack/deploy.sh | 33 ++++++--------------------------- 2 files changed, 13 insertions(+), 30 deletions(-) diff --git a/Dockerfile b/Dockerfile index 41cf8dd..17513e1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,11 +2,15 @@ ARG GO_VERSION=1.18 # First stage: build the executable. -FROM golang:${GO_VERSION}-stretch AS builder +FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-stretch AS builder + +# Expose global args +ARG TARGETARCH +ARG TARGETOS # What arch is it? -ARG GOARCH=amd64 -ARG GOOS=linux +ARG GOARCH=$TARGETARCH +ARG GOOS=$TARGETOS # Install the Certificate-Authority certificates for the app to be able to make # calls to HTTPS endpoints. diff --git a/hack/deploy.sh b/hack/deploy.sh index c09d497..42ac22d 100755 --- a/hack/deploy.sh +++ b/hack/deploy.sh @@ -1,33 +1,12 @@ #!/bin/bash -arch_list="amd64 arm arm64 ppc64le s390x" +arch_list=(amd64 arm arm64 ppc64le s390x) manifest="kubernetesui/metrics-scraper"; -manifest_list=""; +# Concat arch_list with , +platforms=$(IFS=,; echo "${arch_list[*]}") -for i in ${arch_list}; do - # If it's a tagged release, use the tag - # Otherwise, assume it's HEAD and push to latest - container="${manifest}-${i}:${TRAVIS_TAG:="latest"}" +image_name="${manifest}:${TRAVIS_TAG:="latest"}" - echo "--- docker build --no-cache --build-arg GOARCH=${i} -t ${container} ."; - docker build --no-cache --build-arg GOARCH=${i} -t ${container} . - - echo "--- docker push ${container}" - docker push ${container} - - manifest_list="${manifest_list} ${container}" -done; - -echo "--- docker manifest create --amend ${manifest}:${TRAVIS_TAG:="latest"} ${manifest_list}" -docker manifest create --amend ${manifest}:${TRAVIS_TAG:="latest"} ${manifest_list} - -for i in ${arch_list}; do - container="${manifest}-${i}:${TRAVIS_TAG:="latest"}" - - echo "--- docker manifest annotate ${manifest}:${TRAVIS_TAG:="latest"} ${container} --os linux --arch ${i}" - docker manifest annotate ${manifest}:${TRAVIS_TAG:="latest"} ${container} --os linux --arch ${i} -done; - -echo "--- docker manifest push ${manifest}:${TRAVIS_TAG:="latest"}" -docker manifest push ${manifest}:${TRAVIS_TAG:="latest"} +echo "--- docker buildx build --push --platform $platforms --tag $image_name ."; +docker buildx build --push --platform ${platforms} --tag ${image_name} .