Ver Fonte

ci(docker): build image on PRs and publish multi-arch to GHCR on tags (#2105)

Validation-only docker build (amd64) on pull_requests that touch the Docker
surface, and a linux/amd64 + linux/arm64 buildx push to GHCR on v* tags,
tagged semver, semver majors and latest. The workflow is identical on forks
and upstream: the image name comes from github.repository lowercased, and
the built-in GITHUB_TOKEN with packages:write is enough, so no registry
secrets are required. Build args resolve VERSION from the tag minus the v
prefix (same value goreleaser feeds httpapi.Version), BUILD_COMMIT from
GITHUB_SHA and AGENT_VERSION from agent/VERSION, matching the release
stamps. armv7 is not published for now: the QEMU-emulated agent build adds
a lot of CI time for a small audience (release binaries cover it); caching
uses the GitHub Actions cache.

Co-authored-by: Nacho <vnacho@proton.me>
Evgeniy há 2 semanas atrás
pai
commit
80bcbda33e
1 ficheiros alterados com 97 adições e 0 exclusões
  1. 97 0
      .github/workflows/docker.yml

+ 97 - 0
.github/workflows/docker.yml

@@ -0,0 +1,97 @@
+name: docker
+
+# Docker image pipeline (optional install path, docs/docker.md).
+#  - pull_request: builds the server image on amd64 to keep the Dockerfile
+#    honest; publishes nothing.
+#  - v* tag: builds linux/amd64 + linux/arm64 and pushes to GHCR, tagged
+#    semver + latest. The workflow is identical on forks and upstream:
+#    images land under ghcr.io/<owner>/<repo> from github.repository, and
+#    the built-in GITHUB_TOKEN is enough (no registry secrets).
+
+on:
+  pull_request:
+    paths:
+      - 'Dockerfile'
+      - '.dockerignore'
+      - 'docker-compose.yml'
+      - 'scripts/build-agents.sh'
+      - '.github/workflows/docker.yml'
+  push:
+    tags: ['v*']
+
+concurrency:
+  group: docker-${{ github.event.pull_request.number || github.ref }}
+  cancel-in-progress: true
+
+permissions:
+  contents: read
+  packages: write
+
+env:
+  REGISTRY: ghcr.io
+
+jobs:
+  image:
+    runs-on: ubuntu-latest
+    steps:
+      - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
+
+      - uses: docker/setup-qemu-action@99012661954931238ded8c8b007157a8430204e1 # v4.4.0
+      - uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
+
+      # Image name lowercased (GHCR rejects uppercase), version from the tag
+      # minus the v prefix (same value goreleaser feeds httpapi.Version), and
+      # the agent version from agent/VERSION (#790) so the embed stamp
+      # matches the release binaries.
+      - name: Resolve image, version and agent version
+        id: meta
+        env:
+          REPO: ${{ github.repository }}
+        run: |
+          set -e
+          IMAGE="$REGISTRY/$(echo "$REPO" | tr '[:upper:]' '[:lower:]')"
+          echo "image=$IMAGE" >> "$GITHUB_OUTPUT"
+          echo "agent=$(tr -d '[:space:]' < agent/VERSION)" >> "$GITHUB_OUTPUT"
+          echo "commit=${GITHUB_SHA::7}" >> "$GITHUB_OUTPUT"
+          if [ "${{ github.ref_type }}" = 'tag' ]; then
+            VERSION="${GITHUB_REF_NAME#v}"
+            echo "version=$VERSION" >> "$GITHUB_OUTPUT"
+            echo "platforms=linux/amd64,linux/arm64" >> "$GITHUB_OUTPUT"
+            echo "push=true" >> "$GITHUB_OUTPUT"
+            MAJMIN="${VERSION%.*}"
+            MAJ="${VERSION%%.*}"
+            {
+              echo "tags<<EOF"
+              echo "$IMAGE:$VERSION"
+              echo "$IMAGE:$MAJMIN"
+              echo "$IMAGE:$MAJ"
+              echo "$IMAGE:latest"
+              echo "EOF"
+            } >> "$GITHUB_OUTPUT"
+          else
+            echo "version=0.0.0-pr" >> "$GITHUB_OUTPUT"
+            echo "platforms=linux/amd64" >> "$GITHUB_OUTPUT"
+            echo "push=false" >> "$GITHUB_OUTPUT"
+            echo "tags=$IMAGE:pr-validation" >> "$GITHUB_OUTPUT"
+          fi
+
+      - name: Log in to GHCR (tag builds only)
+        if: github.ref_type == 'tag'
+        uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4.8.0
+        with:
+          registry: ${{ env.REGISTRY }}
+          username: ${{ github.actor }}
+          password: ${{ secrets.GITHUB_TOKEN }}
+
+      - uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0
+        with:
+          context: .
+          platforms: ${{ steps.meta.outputs.platforms }}
+          push: ${{ steps.meta.outputs.push }}
+          tags: ${{ steps.meta.outputs.tags }}
+          build-args: |
+            VERSION=${{ steps.meta.outputs.version }}
+            BUILD_COMMIT=${{ steps.meta.outputs.commit }}
+            AGENT_VERSION=${{ steps.meta.outputs.agent }}
+          cache-from: type=gha
+          cache-to: type=gha,mode=max