From 9f7c1804b2935db85e3ea64d373153c12c4f36c8 Mon Sep 17 00:00:00 2001 From: Daniel Aloni Date: Wed, 15 Mar 2023 16:19:22 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20CD=20build.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .cloudbuild/dev.yaml | 2 +- Dockerfile | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/.cloudbuild/dev.yaml b/.cloudbuild/dev.yaml index 8af553094..4f1b6c96c 100644 --- a/.cloudbuild/dev.yaml +++ b/.cloudbuild/dev.yaml @@ -1,6 +1,6 @@ steps: - name: gcr.io/cloud-builders/docker - args: ['build', '-t', 'gcr.io/$PROJECT_ID/dendrite-monolith:$COMMIT_SHA', '-f', 'Dockerfile', '.'] + args: ['build', '--build-arg', 'BUILDPLATFORM=linux/amd64', '-t', 'gcr.io/$PROJECT_ID/dendrite-monolith:$COMMIT_SHA', '-f', 'Dockerfile', '.'] - name: gcr.io/cloud-builders/kubectl args: ['-n', 'dendrite', 'set', 'image', 'deployment/dendrite', 'dendrite=gcr.io/$PROJECT_ID/dendrite-monolith:$COMMIT_SHA'] env: diff --git a/Dockerfile b/Dockerfile index d0db9de49..76c7b7785 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,8 +4,23 @@ FROM --platform=${BUILDPLATFORM} docker.io/golang:1.20-alpine AS base RUN apk --update --no-cache add bash build-base curl -WORKDIR /build -COPY . /build +# +# build creates all needed binaries +# +FROM --platform=${BUILDPLATFORM} base AS build +WORKDIR /src +ARG TARGETOS +ARG TARGETARCH +ARG FLAGS +RUN --mount=target=. \ + --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/go/pkg/mod \ + USERARCH=`go env GOARCH` \ + GOARCH="$TARGETARCH" \ + GOOS="linux" \ + CGO_ENABLED=$([ "$TARGETARCH" = "$USERARCH" ] && echo "1" || echo "0") \ + go build -v -ldflags="${FLAGS}" -trimpath -o /out/ ./cmd/... + # # Builds the Dendrite image containing all required binaries