From c5be9119bc11fcb8030e2c054530db26de3bbc87 Mon Sep 17 00:00:00 2001 From: YuCat <66810215+YuCat-OVO@users.noreply.github.com> Date: Sun, 5 May 2024 23:42:03 +0800 Subject: [PATCH] Build --- .github/workflows/build.yml | 95 +++++++++++++++++++++++++++++-------- .github/workflows/test.yml | 9 ++-- 2 files changed, 81 insertions(+), 23 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2f0fc74..9de72ce 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -27,21 +27,10 @@ jobs: prepare: name: prepare runs-on: ubuntu-latest - strategy: - fail-fast: false - matrix: - platform: ["linux/amd64", "linux/arm64"] - steps: - name: Checkout uses: actions/checkout@master - - name: Set variables - id: set-vars - run: | - echo "TARGET=$(echo ${{ matrix.platform }} | sed 's#/#-#g')" >> "$GITHUB_OUTPUT" - echo "ARCH=$(echo ${{ matrix.platform }} | sed 's#linux/##g')" >> "$GITHUB_OUTPUT" - - name: Docker Meta uses: docker/metadata-action@v5 id: meta @@ -50,8 +39,8 @@ jobs: ${{ env.DOCKERHUB_SLUG }} ${{ env.GHCR_SLUG }} tags: | - type=ref,event=branch,prefix=br-,enable=${{ github.ref_name != github.event.repository.default_branch }} - type=edge,branch=develop,prefix=edge- + type=ref,event=branch,enable=${{ github.ref_name != github.event.repository.default_branch }} + type=edge,branch=develop type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }} type=sha labels: | @@ -63,9 +52,43 @@ jobs: run: | mv "${{ steps.meta.outputs.bake-file }}" "/tmp/bake-meta.json" - - name: Print meta bake definition file + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Upload meta bake definition + uses: actions/upload-artifact@v4 + with: + name: bake-meta + path: /tmp/bake-meta.json + if-no-files-found: error + retention-days: 1 + + build: + runs-on: ubuntu-latest + needs: + - prepare + strategy: + fail-fast: false + matrix: + platform: ["linux/amd64", "linux/arm64"] + steps: + - name: Prepare run: | - cat "/tmp/bake-meta.json" + platform=${{ matrix.platform }} + echo "PLATFORM_PAIR=${platform//\//-}" >> "$GITHUB_ENV" + echo "ARCH=${platform//linux\//}" >> "$GITHUB_ENV" + + - name: Checkout + uses: actions/checkout@v4 + + - name: Download meta bake definition + uses: actions/download-artifact@v4 + with: + name: bake-meta + path: /tmp - name: Set up QEMU uses: docker/setup-qemu-action@v3 @@ -73,12 +96,46 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 with: - platforms: ${{ matrix.platform }} + buildkitd-flags: "--debug" - - name: Upload meta bake definition + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_TOKEN }} + + - name: Login to GHCR + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build + id: bake + uses: docker/bake-action@v4 + with: + files: | + ./docker-bake.hcl + /tmp/bake-meta.json + targets: ${{ env.PLATFORM_PAIR }} + set: | + *.tags= + *.platform=${{ matrix.platform }} + *.cache-from=type=gha,scope=build-${{ env.PLATFORM_PAIR }} + *.cache-to=type=gha,scope=build-${{ env.PLATFORM_PAIR }} + *.output=type=image,"name=${{ env.DOCKERHUB_SLUG }},${{ env.GHCR_SLUG }}",push-by-digest=true,name-canonical=true,push=true + + - name: Export digest + run: | + mkdir -p /tmp/digests + digest="${{ fromJSON(steps.bake.outputs.metadata).image['containerimage.digest'] }}" + touch "/tmp/digests/${digest#sha256:}" + + - name: Upload digest uses: actions/upload-artifact@v4 with: - name: bake-meta-${{ steps.set-vars.outputs.ARCH }} - path: /tmp/bake-meta.json + name: digests-${{ env.PLATFORM_PAIR }} + path: /tmp/digests/* if-no-files-found: error retention-days: 1 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 423503c..a322d5d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -32,8 +32,9 @@ jobs: - name: Set the value id: set-vars run: | - echo "TARGET=$(echo ${{ matrix.platform }} | sed 's#/#-#g')" >> "$GITHUB_OUTPUT" - echo "ARCH=$(echo ${{ matrix.platform }} | sed 's#linux/##g')" >> "$GITHUB_OUTPUT" + platform=${{ matrix.platform }} + echo "PLATFORM_PAIR=${platform//\//-}" >> "$GITHUB_ENV" + echo "ARCH=${platform//linux\//}" >> "$GITHUB_ENV" - name: Set up QEMU uses: docker/setup-qemu-action@v3 @@ -48,9 +49,9 @@ jobs: uses: docker/bake-action@v4 with: push: true - targets: ${{ steps.set-vars.outputs.TARGET }} + targets: ${{ env.TARGET }} env: - DEFAULT_TAG: ${{ env.BUILD_TAG }}-${{ steps.set-vars.outputs.TARGET }} + DEFAULT_TAG: ${{ env.BUILD_TAG }}-${{ env.PLATFORM_PAIR }} - name: Test run: |