From 15dae35f63fdc4015bc037d10907bb12836f68c4 Mon Sep 17 00:00:00 2001 From: Timo Naroska Date: Tue, 23 Apr 2024 07:21:46 -0700 Subject: [PATCH] Feat: multi-arch docker image (#1756) * Feat: multi-arch docker image - adapt Dockerfile to support cross-compilation depending on TARGETARCH and TARGETOS variables see https://www.docker.com/blog/faster-multi-platform-builds-dockerfile-cross-compilation-guide/ - set target platforms for docker/build-push-action * Support running on forks * Fix ARG format * Fix docker digest step * Restrict permissions * Update action versions * Set $TARGETPLATFORM explicitly https://github.com/docker/build-push-action/issues/820#issuecomment-1486849546 --------- Co-authored-by: Norman Gehrsitz <45375059+ngehrsitz@users.noreply.github.com> --- .github/workflows/docker.yml | 20 +++++++++++++------- Dockerfile | 10 ++++++++-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index a7e77a6cc..9be444b25 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -5,19 +5,23 @@ on: tags: - 'v*' +permissions: + contents: read + packages: write + jobs: docker-build: runs-on: ubuntu-latest steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Set up Docker Buildx id: buildx - uses: docker/setup-buildx-action@v2 + uses: docker/setup-buildx-action@v3 - name: Login to Github Packages - uses: docker/login-action@v2 + uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.actor }} @@ -25,18 +29,20 @@ jobs: - name: Docker meta id: meta - uses: docker/metadata-action@v4 + uses: docker/metadata-action@v5 with: images: ghcr.io/swaggo/swag - name: Build image and push to GitHub Container Registry - uses: docker/build-push-action@v2 + id: docker_build + uses: docker/build-push-action@v5 with: context: . + platforms: linux/amd64,linux/arm64 push: true tags: | - ghcr.io/swaggo/swag:latest - ghcr.io/swaggo/swag:${{github.ref_name}} + ghcr.io/${{ github.repository }}:latest + ghcr.io/${{ github.repository }}:${{github.ref_name}} labels: ${{ steps.meta.outputs.labels }} - name: Image digest diff --git a/Dockerfile b/Dockerfile index 0c1e97716..5ea913434 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ # Dockerfile References: https://docs.docker.com/engine/reference/builder/ # Start from the latest golang base image -FROM golang:1.21-alpine as builder +FROM --platform=$BUILDPLATFORM golang:1.21-alpine as builder # Set the Current Working Directory inside the container WORKDIR /app @@ -15,12 +15,18 @@ RUN go mod download # Copy the source from the current directory to the Working Directory inside the container COPY . . +# Configure go compiler target platform +ARG TARGETOS +ARG TARGETARCH +ENV GOARCH=$TARGETARCH \ + GOOS=$TARGETOS + # Build the Go app RUN CGO_ENABLED=0 GOOS=linux go build -v -a -installsuffix cgo -o swag cmd/swag/main.go ######## Start a new stage from scratch ####### -FROM scratch +FROM --platform=$TARGETPLATFORM scratch WORKDIR /code/