From a5ea47afc91bebfa1229263e41b7aac031c49576 Mon Sep 17 00:00:00 2001 From: garder500 Date: Tue, 28 Oct 2025 18:39:11 +0100 Subject: Ajouter un workflow de construction pour les binaires Go et les images Docker multi-architecture --- .github/workflows/build.yml | 164 ++++++++++++++++++++++++++++++++++++++++++++ Dockerfile | 6 +- 2 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/build.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..5732360 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,164 @@ +name: Build + +on: + push: + branches: [ main, develop ] + pull_request: + branches: [ main, develop ] + workflow_dispatch: + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + # Build Go binaries for multiple platforms + build-binaries: + name: Build Go Binary + runs-on: ubuntu-latest + strategy: + matrix: + include: + # Linux AMD64 + - goos: linux + goarch: amd64 + output: sovrabase-linux-amd64 + # Linux ARM64 + - goos: linux + goarch: arm64 + output: sovrabase-linux-arm64 + # macOS AMD64 (Intel) + - goos: darwin + goarch: amd64 + output: sovrabase-darwin-amd64 + # macOS ARM64 (Apple Silicon) + - goos: darwin + goarch: arm64 + output: sovrabase-darwin-arm64 + # Windows AMD64 + - goos: windows + goarch: amd64 + output: sovrabase-windows-amd64.exe + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: '1.25.2' + cache: true + + - name: Download dependencies + run: go mod download + + - name: Build binary + env: + GOOS: ${{ matrix.goos }} + GOARCH: ${{ matrix.goarch }} + CGO_ENABLED: 0 + run: | + go build -ldflags="-w -s" -o ${{ matrix.output }} ./cmd/server/main.go + + - name: Upload binary artifact + uses: actions/upload-artifact@v4 + with: + name: ${{ matrix.output }} + path: ${{ matrix.output }} + retention-days: 7 + + # Build Docker images for multiple architectures + build-docker: + name: Build Docker Image + strategy: + matrix: + include: + # AMD64 on Ubuntu + - runner: ubuntu-latest + platform: linux/amd64 + arch: amd64 + # ARM64 on Ubuntu ARM runner + - runner: ubuntu-24.04-arm + platform: linux/arm64 + arch: arm64 + + runs-on: ${{ matrix.runner }} + + permissions: + contents: read + packages: write + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to Container Registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=ref,event=branch + type=ref,event=pr + type=sha,prefix={{branch}}- + type=raw,value=latest,enable={{is_default_branch}} + + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + platforms: ${{ matrix.platform }} + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + build-args: | + TARGETARCH=${{ matrix.arch }} + cache-from: type=gha + cache-to: type=gha,mode=max + provenance: false + + # Create manifest list to combine both architectures + create-manifest: + name: Create Multi-arch Manifest + needs: build-docker + runs-on: ubuntu-latest + if: github.event_name != 'pull_request' + + permissions: + contents: read + packages: write + + steps: + - name: Log in to Container Registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Create and push manifest + run: | + # Extract branch or tag name + BRANCH=${GITHUB_REF##*/} + SHA_SHORT=$(echo $GITHUB_SHA | cut -c1-7) + + # Create manifest for branch tag + docker buildx imagetools create -t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${BRANCH} \ + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${BRANCH}-${SHA_SHORT} + + # Create manifest for latest tag if on default branch + if [ "${{ github.ref }}" = "refs/heads/main" ] || [ "${{ github.ref }}" = "refs/heads/master" ]; then + docker buildx imagetools create -t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest \ + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${BRANCH}-${SHA_SHORT} + fi diff --git a/Dockerfile b/Dockerfile index 9fffe62..920a40e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,9 @@ # Stage 1: Build FROM golang:1.25.2-alpine AS builder +# Build arguments for multi-architecture support +ARG TARGETARCH + # Install build dependencies RUN apk add --no-cache git ca-certificates tzdata @@ -18,7 +21,8 @@ COPY . . # Build the application # CGO_ENABLED=0 for static binary # -ldflags="-w -s" to strip debug info and reduce binary size -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \ +# TARGETARCH will be automatically set by Docker buildx (amd64 or arm64) +RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} go build \ -ldflags="-w -s" \ -o sovrabase \ ./cmd/server/main.go -- cgit v1.2.3