From 588b0b0721a4bfd51c7afbfa1a857b6c5dc1e428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Batuhan=20Apayd=C4=B1n?= Date: Thu, 25 Jan 2024 23:28:07 +0300 Subject: [PATCH] Feature/enhance multi platform build (#66) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Batuhan Apaydın Co-authored-by: Furkan Turkal --- .github/workflows/create-images.yml | 3 +++ docker/exporter/Dockerfile | 22 +++++++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/.github/workflows/create-images.yml b/.github/workflows/create-images.yml index c2bde8f..b519d0c 100644 --- a/.github/workflows/create-images.yml +++ b/.github/workflows/create-images.yml @@ -3,6 +3,7 @@ name: Create images on: release: types: [published] + workflow_dispatch: permissions: contents: write @@ -62,6 +63,8 @@ jobs: push: true tags: | ${{ env.REGISTRY }}/${{ matrix.apps }}:${{ env.IMG_TAG }} + build-args: | + GO_VERSION=${{ env.GO_VERSION }} labels: | org.opencontainers.image.source=${{ github.repositoryUrl }} diff --git a/docker/exporter/Dockerfile b/docker/exporter/Dockerfile index 91371c4..c358c28 100644 --- a/docker/exporter/Dockerfile +++ b/docker/exporter/Dockerfile @@ -1,26 +1,30 @@ +# syntax=docker/dockerfile:1 + # Build the manager binary -FROM golang:1.19 as builder +ARG GO_VERSION=1.19 +FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} as builder WORKDIR /workspace # Copy the Go Modules manifests -COPY go.mod go.mod -COPY go.sum go.sum -# cache deps before building and copying source so that we don't need to re-download as much -# and so that source changes don't invalidate our downloaded layer -RUN go mod download +RUN --mount=type=cache,target=/go/pkg/mod/ \ + --mount=type=bind,source=go.sum,target=go.sum \ + --mount=type=bind,source=go.mod,target=go.mod \ + go mod download -x # Copy the go source COPY cmd/exporter/ cmd/exporter/ COPY pkg/ pkg/ # Build -ARG TARGETARCH -RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} GO111MODULE=on go build -o exporter cmd/exporter/main.go +ARG TARGETOS TARGETARCH +RUN --mount=type=cache,target=/go/pkg/mod/ \ + --mount=type=bind,target=. \ + CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH GO111MODULE=on go build -o /bin/exporter cmd/exporter/main.go # Use distroless as minimal base image to package the exporter binary # Refer to https://github.com/GoogleContainerTools/distroless for more details FROM --platform=${TARGETPLATFORM:-linux/amd64} gcr.io/distroless/static:nonroot WORKDIR / -COPY --from=builder /workspace/exporter . +COPY --from=builder /bin/exporter . ENTRYPOINT ["/exporter"] \ No newline at end of file