name: Docker on: push: branches: [main] tags: ['v*.*.*'] pull_request: branches: [main] permissions: contents: read packages: write env: IMAGE: ghcr.io/${{ github.repository_owner }}/freellmapi # Each platform builds NATIVELY (ubuntu-24.04-arm is free for public repos) — # no QEMU. Emulated arm64 builds took ~20 min cold (better-sqlite3 compiles # via node-gyp) and their giant gha-cache exports hit backend errors; native # legs build in ~3 min with small per-arch caches. Pushes are by digest, then # a merge job stitches the multi-arch manifest. PRs only build (no push). jobs: build: name: Build (${{ matrix.platform }}) runs-on: ${{ matrix.runner }} strategy: fail-fast: false matrix: include: - platform: linux/amd64 runner: ubuntu-24.04 - platform: linux/arm64 runner: ubuntu-24.04-arm steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 with: # Shared runner IPs can exhaust Docker Hub's anonymous pull quota. # Use Google's public cache for base images and the Dockerfile frontend; # BuildKit retains Docker Hub as the fallback on cache misses. buildkitd-config-inline: | [registry."docker.io"] mirrors = ["mirror.gcr.io"] - name: Platform slug run: | platform="${{ matrix.platform }}" echo "PLATFORM_PAIR=${platform//\//-}" >> "$GITHUB_ENV" - name: Log in to GHCR if: github.event_name != 'pull_request' uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Docker metadata id: meta uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6.2.0 with: images: ${{ env.IMAGE }} - name: Build (and push by digest) id: build uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0 with: context: . file: ./Dockerfile platforms: ${{ matrix.platform }} build-args: | FREELLMAPI_COMMIT_SHA=${{ github.sha }} labels: ${{ steps.meta.outputs.labels }} cache-from: type=gha,scope=${{ env.PLATFORM_PAIR }} cache-to: type=gha,mode=max,scope=${{ env.PLATFORM_PAIR }} outputs: type=image,name=${{ env.IMAGE }},push-by-digest=true,name-canonical=true,push=${{ github.event_name != 'pull_request' }} - name: Export digest if: github.event_name != 'pull_request' run: | mkdir -p "${{ runner.temp }}/digests" digest="${{ steps.build.outputs.digest }}" touch "${{ runner.temp }}/digests/${digest#sha256:}" - name: Upload digest if: github.event_name != 'pull_request' uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 with: name: digests-${{ env.PLATFORM_PAIR }} path: ${{ runner.temp }}/digests/* if-no-files-found: error retention-days: 1 merge: name: Merge manifest and push if: github.event_name != 'pull_request' runs-on: ubuntu-24.04 needs: [build] steps: - name: Download digests uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 with: path: ${{ runner.temp }}/digests pattern: digests-* merge-multiple: true - uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 - name: Log in to GHCR uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Docker metadata id: meta uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6.2.0 with: images: ${{ env.IMAGE }} tags: | type=ref,event=branch type=ref,event=tag type=sha,prefix=sha- # `latest` must follow the newest RELEASE, not main. This workflow runs # on both main pushes and v* tags, so `{{is_default_branch}}` attached # `latest` to every main build and `docker pull ...:latest` served # unreleased code — the opposite of what the tag conventionally means, # and what sent people to pin `v0.6.5` by hand (discussion #533). # Prereleases (v1.2.3-rc1) are excluded so `latest` stays on stable. # Main builds keep their `main` and `sha-` tags, so anything tracking # the development stream is unaffected. type=raw,value=latest,enable=${{ github.ref_type == 'tag' && !contains(github.ref_name, '-') }} - name: Create manifest list and push working-directory: ${{ runner.temp }}/digests run: | docker buildx imagetools create \ $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ $(printf "${IMAGE}@sha256:%s " *) - name: Inspect image run: docker buildx imagetools inspect "${IMAGE}:${{ steps.meta.outputs.version }}"