From eb45d719f4b6c2ed07733c2bba2dafc8de770b61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hubert=20Zi=C4=99ba?= Date: Mon, 2 Jun 2025 16:57:26 +0200 Subject: [PATCH] Build multiarch Docker image --- .github/workflows/release.yml | 7 +++++++ Dockerfile | 12 ++++++------ Makefile | 17 ++++++++++------- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3da8625..528b785 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -111,12 +111,19 @@ jobs: with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Build and push Docker image id: push uses: docker/build-push-action@v6 with: context: . push: true + platforms: linux/amd64,linux/arm64 tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} diff --git a/Dockerfile b/Dockerfile index 5941ecc..8289961 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,11 @@ FROM golang:latest AS builder +ARG TARGETOS +ARG TARGETARCH + ENV GO111MODULE=on \ + GOOS=${TARGETOS} \ + GOARCH=${TARGETARCH} \ CGO_ENABLED=0 WORKDIR /build @@ -11,16 +16,11 @@ COPY go.sum . RUN go mod download COPY . . -RUN make build +RUN OS=${TARGETOS} ARCH=${TARGETARCH} make build WORKDIR /dist RUN cp /build/cortex-tenant ./cortex-tenant - -RUN ldd cortex-tenant | tr -s '[:blank:]' '\n' | grep '^/' | \ - xargs -I % sh -c 'mkdir -p $(dirname ./%); cp % ./%;' -RUN mkdir -p lib64 && cp /lib64/ld-linux-x86-64.so.2 lib64/ - RUN mkdir /data && cp /build/deploy/cortex-tenant.yml /data/cortex-tenant.yml FROM scratch diff --git a/Makefile b/Makefile index 150bb1c..11be1d0 100644 --- a/Makefile +++ b/Makefile @@ -10,13 +10,16 @@ RELEASE := 1 GO ?= go OUT := .out +OS ?= linux +ARCH ?= amd64 + all: rpm deb build: - go test ./... && \ - GOARCH=amd64 \ - GOOS=linux \ - CGO_ENABLED=0 \ + export GOARCH=$(ARCH) + export GOOS=$(OS) + export CGO_ENABLED=0 + go test ./... $(GO) build -ldflags "-s -w -extldflags \"-static\" -X main.Version=$(VERSION)" prepare: @@ -47,12 +50,12 @@ build-rpm: --iteration $(RELEASE) \ --force \ --rpm-compression bzip2 \ - --rpm-os linux \ + --rpm-os $(OS) \ --url $(URL) \ --description "$(DESCRIPTION)" \ -m "$(MAINTAINER)" \ --license "$(LICENSE)" \ - -a amd64 \ + -a $(ARCH) \ . build-deb: @@ -76,5 +79,5 @@ build-deb: --description "$(DESCRIPTION)" \ -m "$(MAINTAINER)" \ --license "$(LICENSE)" \ - -a amd64 \ + -a $(ARCH) \ .