summaryrefslogtreecommitdiff
path: root/.github/workflows/build.yml
blob: 78323d7c246b7b8b54443ee8dd4fea742374c13b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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,suffix=-${{ matrix.arch }}
            type=ref,event=pr,suffix=-${{ matrix.arch }}
            type=sha,prefix={{branch}}-,suffix=-${{ matrix.arch }}

      - 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 (combines amd64 and arm64)
          docker buildx imagetools create -t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${BRANCH} \
            ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${BRANCH}-${SHA_SHORT}-amd64 \
            ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${BRANCH}-${SHA_SHORT}-arm64

          # 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}-amd64 \
              ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${BRANCH}-${SHA_SHORT}-arm64
          fi