From f365c7f86a54a87cbc5aaa2aa27eb9a5d57bdc94 Mon Sep 17 00:00:00 2001 From: Desmond Cox Date: Sun, 19 May 2024 15:27:49 +0200 Subject: [PATCH] Build a multi-arch Docker image (amd64 and arm64) --- .github/workflows/cd.yml | 21 +++++++++++++++++---- Dockerfile | 6 ++++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 7003c75f..db8302e7 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -3,6 +3,11 @@ name: Publish Docker image on: release: types: [published] + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + TARGET_PLATFORMS: linux/amd64,linux/arm64 jobs: push_to_registry: @@ -12,17 +17,24 @@ jobs: - name: Check out the repo uses: actions/checkout@v4 - - name: Log in to Docker Hub + - name: Log in to the Container registry uses: docker/login-action@v3 with: - username: ${{ secrets.DOCKER_USERNAME }} - password: ${{ secrets.DOCKER_PASSWORD }} + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} - name: Extract metadata (tags, labels) for Docker id: meta uses: docker/metadata-action@v5 with: - images: jgkawell/yarr + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 - name: Build and push Docker image uses: docker/build-push-action@v5 @@ -32,3 +44,4 @@ jobs: push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} + platforms: ${{ env.TARGET_PLATFORMS }} diff --git a/Dockerfile b/Dockerfile index e7a9c9c2..bf0e77b4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:alpine AS build +FROM --platform=$BUILDPLATFORM golang:alpine AS build WORKDIR /app @@ -7,7 +7,9 @@ COPY . . RUN go mod download RUN go mod verify -RUN GOOS=linux GOARCH=amd64 go build -o bin/yarr . +ARG TARGETOS +ARG TARGETARCH +RUN GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o bin/yarr . FROM alpine:latest