Skip to content

Commit

Permalink
Cross build for amd64 and arm64 platforms (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
lomigmegard committed Oct 4, 2024
1 parent 700c375 commit 316a1d1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ jobs:
# https://github.com/docker/setup-buildx-action
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
# Only build for amd64 for now, we should extend to arm64 eventually
platforms: linux/amd64

# Login against a Docker registry except on PR
# https://github.com/docker/login-action
Expand All @@ -59,6 +56,7 @@ jobs:
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
Expand Down
16 changes: 10 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
FROM golang:1.22.5-alpine3.19 AS builder
# Use the $BUILDPLATFORM for the build stage
FROM --platform=$BUILDPLATFORM golang:1.22.5-alpine3.19 AS builder

WORKDIR /app
RUN cat /etc/resolv.conf && apk add --no-cache git && \
git config --add --global url."[email protected]:".insteadOf https://github.com

# Prepare dependencies
COPY go.mod go.sum ./
COPY go.mod go.sum .
RUN go mod download
# Build the binary

# Copy the sources and config
COPY ./cmd ./cmd
COPY ./internal ./internal
COPY ./config ./config
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o api ./cmd/api

# Build the binary based on the target platform
ARG TARGETOS TARGETARCH
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o api ./cmd/api

# Use the $TARGETPLATFORM by default for the runtime stage
FROM alpine:3.19
WORKDIR /app
COPY --from=builder /app/api ./api
Expand Down

0 comments on commit 316a1d1

Please sign in to comment.