From 4a98bd6e5d93406044693401620f2266d258f4a4 Mon Sep 17 00:00:00 2001 From: Ryan Cao <70191398+ryanccn@users.noreply.github.com> Date: Mon, 26 Jun 2023 16:04:59 +0800 Subject: [PATCH] ci: actually build docker images in parallel and install QEMU correctly --- .github/workflows/build.yml | 77 +++++++++++++++++++++++++++++++------ 1 file changed, 66 insertions(+), 11 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0252df71..37d4a3ab 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -33,10 +33,22 @@ jobs: build-docker: runs-on: ubuntu-latest + strategy: + matrix: + platform: + - linux/arm64 + - linux/amd64 steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + if: ${{ matrix.platform != 'linux/amd64' }} + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 - name: Acquire Docker image metadata id: metadata @@ -49,16 +61,59 @@ jobs: with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - - - name: Install QEMU - run: | - sudo apt-get install -y qemu qemu-user-static binfmt-support - - - name: Build and push Docker image - uses: docker/build-push-action@v3 + + - name: Build and push by digest + uses: docker/build-push-action@v4 + id: build with: context: . - platforms: linux/amd64,linux/arm64 - tags: ${{ steps.metadata.outputs.tags }} labels: ${{ steps.metadata.outputs.labels }} - push: true + platforms: ${{ matrix.platform }} + outputs: type=image,name=valeriansaliou/sonic,push-by-digest=true,name-canonical=true,push=true + + - name: Export digests + run: | + mkdir -p /tmp/digests + digest="${{ steps.build.outputs.digest }}" + touch "/tmp/digests/${digest#sha256:}" + + - name: Upload digests + uses: actions/upload-artifact@v3 + with: + name: docker-digests + path: /tmp/digests/* + if-no-files-found: error + retention-days: 1 + + merge-docker: + runs-on: ubuntu-latest + needs: + - build-docker + + steps: + - name: Download digests + uses: actions/download-artifact@v3 + with: + name: docker-digests + path: /tmp/digests + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Login to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v4 + with: + images: valeriansaliou/sonic + + - name: Create manifest list and push + working-directory: /tmp/digests + run: | + docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ + $(printf 'valeriansaliou/sonic@sha256:%s ' *)