Skip to content

Commit

Permalink
Feat: multi-arch docker image (#1756)
Browse files Browse the repository at this point in the history
* 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

docker/build-push-action#820 (comment)

---------

Co-authored-by: Norman Gehrsitz <[email protected]>
  • Loading branch information
tnaroska and ngehrsitz authored Apr 23, 2024
1 parent 85254b4 commit 15dae35
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
20 changes: 13 additions & 7 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,44 @@ 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 }}
password: ${{ secrets.GITHUB_TOKEN }}

- 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
Expand Down
10 changes: 8 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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/

Expand Down

0 comments on commit 15dae35

Please sign in to comment.