Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cross build for amd64 and arm64 platforms #8

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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