Skip to content

Commit e7b5d09

Browse files
committed
Optimize Docker build for faster multi-platform images.
- Use native Go cross-compilation instead of QEMU emulation. - Switch to scratch base image for minimal size. - Add GHA caching for Docker builds. - Add latest tag for main branch builds. - Upgrade to Go 1.25.4.
1 parent 28d6ea1 commit e7b5d09

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

.github/workflows/docker-publish.yml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ on:
44
workflow_dispatch:
55
push:
66
branches: [main]
7-
# Publish semver tags as releases.
87
tags: ["v*.*.*"]
98

109
env:
@@ -22,29 +21,43 @@ jobs:
2221
steps:
2322
- name: Checkout repository
2423
uses: actions/checkout@v6
24+
2525
- name: Set up Docker Buildx
2626
uses: docker/setup-buildx-action@v3
27+
2728
- name: Log in to the Container registry
29+
if: github.event_name != 'pull_request'
2830
uses: docker/login-action@v3
2931
with:
3032
registry: ${{ env.REGISTRY }}
3133
username: ${{ github.actor }}
3234
password: ${{ secrets.GITHUB_TOKEN }}
35+
3336
- name: Extract metadata (tags, labels) for Docker
3437
id: meta
3538
uses: docker/metadata-action@v5
3639
with:
3740
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
41+
tags: |
42+
type=ref,event=branch
43+
type=semver,pattern={{version}}
44+
type=semver,pattern={{major}}.{{minor}}
45+
type=raw,value=latest,enable={{is_default_branch}}
46+
3847
- name: Build and push Docker image
3948
id: push
4049
uses: docker/build-push-action@v6
4150
with:
4251
context: .
4352
platforms: linux/amd64,linux/arm64
44-
push: true
53+
push: ${{ github.event_name != 'pull_request' }}
4554
tags: ${{ steps.meta.outputs.tags }}
4655
labels: ${{ steps.meta.outputs.labels }}
56+
cache-from: type=gha
57+
cache-to: type=gha,mode=max
58+
4759
- name: Generate artifact attestation
60+
if: github.event_name != 'pull_request'
4861
uses: actions/attest-build-provenance@v3
4962
with:
5063
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}

Dockerfile

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
# Build stage.
2-
FROM golang:alpine AS builder
2+
FROM --platform=$BUILDPLATFORM golang:1.25-alpine AS builder
33
WORKDIR /app
4+
5+
# Cache Go modules.
46
COPY go.mod go.sum ./
57
RUN go mod download
8+
9+
# Build for target platform.
10+
ARG TARGETOS TARGETARCH
611
COPY . .
7-
RUN go build -o main cmd/supermarket/*
12+
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o main cmd/supermarket/*
813

9-
# Runner stage.
10-
FROM alpine:latest AS runner
11-
RUN apk --no-cache add ca-certificates
12-
WORKDIR /app
13-
COPY --from=builder /app/main .
14-
ENTRYPOINT ["./main"]
14+
# Runner stage - use scratch for minimal image.
15+
FROM scratch AS runner
16+
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
17+
COPY --from=builder /app/main /main
18+
ENTRYPOINT ["/main"]

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/csobrinho/supermarket-api
22

3-
go 1.24.3
3+
go 1.25.4
44

55
require (
66
github.com/google/logger v1.1.1

0 commit comments

Comments
 (0)