diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 739b7fd..125fecc 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -14,22 +14,10 @@ env: jobs: build: - name: Build (${{ matrix.platform }}) - runs-on: ${{ matrix.runner }} + runs-on: ubuntu-latest permissions: contents: read packages: write - outputs: - digest-amd64: ${{ steps.digest.outputs.digest-linux-amd64 }} - digest-arm64: ${{ steps.digest.outputs.digest-linux-arm64 }} - strategy: - fail-fast: false - matrix: - include: - - platform: linux/amd64 - runner: ubuntu-latest - - platform: linux/arm64 - runner: ubuntu-24.04-arm steps: - name: Checkout @@ -39,6 +27,11 @@ jobs: id: image run: echo "name=$(echo '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}' | tr '[:upper:]' '[:lower:]')" >> "$GITHUB_OUTPUT" + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + with: + platforms: arm64 + - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 @@ -50,74 +43,6 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Extract metadata - id: meta - uses: docker/metadata-action@v5 - with: - images: ${{ steps.image.outputs.name }} - - - name: Build and push by digest - id: build - uses: docker/build-push-action@v6 - with: - context: . - platforms: ${{ matrix.platform }} - push: ${{ github.event_name != 'pull_request' }} - labels: ${{ steps.meta.outputs.labels }} - outputs: type=image,name=${{ steps.image.outputs.name }},push-by-digest=true,name-canonical=true,push=${{ github.event_name != 'pull_request' }} - cache-from: type=gha,scope=${{ matrix.platform }} - cache-to: type=gha,mode=max,scope=${{ matrix.platform }} - provenance: false - - - name: Export digest - if: github.event_name != 'pull_request' - id: digest - run: | - PLATFORM_SAFE=$(echo "${{ matrix.platform }}" | tr '/' '-') - echo "digest-${PLATFORM_SAFE}=${{ steps.build.outputs.digest }}" >> "$GITHUB_OUTPUT" - mkdir -p /tmp/digests - echo "${{ steps.build.outputs.digest }}" > "/tmp/digests/${PLATFORM_SAFE}.txt" - - - name: Upload digest artifact - if: github.event_name != 'pull_request' - uses: actions/upload-artifact@v4 - with: - name: digest-${{ matrix.runner }} - path: /tmp/digests/ - if-no-files-found: error - retention-days: 1 - - merge: - name: Merge manifests - runs-on: ubuntu-latest - if: github.event_name != 'pull_request' - needs: build - permissions: - contents: read - packages: write - - steps: - - name: Download digests - uses: actions/download-artifact@v4 - with: - pattern: digest-* - path: /tmp/digests - merge-multiple: true - - - name: Set lowercase image name - id: image - run: echo "name=$(echo '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}' | tr '[:upper:]' '[:lower:]')" >> "$GITHUB_OUTPUT" - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - - name: Login to GitHub 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 @@ -130,12 +55,15 @@ jobs: type=semver,pattern={{major}}.{{minor}} type=sha,prefix= - - name: Create and push manifest - run: | - DIGESTS=$(find /tmp/digests -name '*.txt' -exec cat {} \; | xargs -I{} echo "${{ steps.image.outputs.name }}@{}") - TAGS=$(echo "${{ steps.meta.outputs.tags }}" | xargs -I{} echo "--tag {}") - docker buildx imagetools create $TAGS $DIGESTS + - name: Build and push + uses: docker/build-push-action@v6 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max + provenance: false - - name: Inspect manifest - run: | - docker buildx imagetools inspect ${{ steps.image.outputs.name }}:${{ steps.meta.outputs.version }} diff --git a/Dockerfile b/Dockerfile index dedb35c..7c6cfa4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,8 @@ -FROM golang:1.21-alpine AS builder +# builder 阶段始终运行在构建机原生平台(amd64),用 Go 交叉编译目标平台二进制 +FROM --platform=$BUILDPLATFORM golang:1.21-alpine AS builder + +ARG TARGETOS +ARG TARGETARCH WORKDIR /app COPY go.mod go.sum ./ @@ -8,7 +12,7 @@ RUN --mount=type=cache,target=/go/pkg/mod \ COPY . . RUN --mount=type=cache,target=/go/pkg/mod \ --mount=type=cache,target=/root/.cache/go-build \ - CGO_ENABLED=0 GOOS=linux go build -o kiro-go . + CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o kiro-go . FROM alpine:latest RUN apk --no-cache add ca-certificates