|
| 1 | +name: Build Docker Image |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + |
| 6 | +env: |
| 7 | + GITHUB_CR_REPO: ghcr.io/${{ github.repository }} |
| 8 | + |
| 9 | +jobs: |
| 10 | + build: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + strategy: |
| 13 | + fail-fast: false |
| 14 | + matrix: |
| 15 | + platform: |
| 16 | + - linux/amd64 |
| 17 | + - linux/arm64 |
| 18 | + steps: |
| 19 | + - name: Prepare |
| 20 | + run: | |
| 21 | + platform=${{ matrix.platform }} |
| 22 | + echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV |
| 23 | +
|
| 24 | + - name: Docker meta |
| 25 | + id: meta |
| 26 | + uses: docker/metadata-action@v5 |
| 27 | + with: |
| 28 | + images: | |
| 29 | + ${{ env.GITHUB_CR_REPO }} |
| 30 | +
|
| 31 | + - name: Login to GHCR |
| 32 | + uses: docker/login-action@v3 |
| 33 | + with: |
| 34 | + registry: ghcr.io |
| 35 | + username: ${{ github.repository_owner }} |
| 36 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 37 | + |
| 38 | + - name: Set up QEMU |
| 39 | + uses: docker/setup-qemu-action@v3 |
| 40 | + |
| 41 | + - name: Set up Docker Buildx |
| 42 | + uses: docker/setup-buildx-action@v3 |
| 43 | + |
| 44 | + - name: Build and push by digest |
| 45 | + id: build |
| 46 | + uses: docker/build-push-action@v6 |
| 47 | + with: |
| 48 | + platforms: ${{ matrix.platform }} |
| 49 | + labels: ${{ steps.meta.outputs.labels }} |
| 50 | + tags: | |
| 51 | + ${{ env.GITHUB_CR_REPO }} |
| 52 | + build-args: | |
| 53 | + TARGETPLATFORM=${{ matrix.platform }} |
| 54 | + outputs: type=image,push-by-digest=true,name-canonical=true,push=true |
| 55 | + |
| 56 | + - name: Export digest |
| 57 | + run: | |
| 58 | + mkdir -p ${{ runner.temp }}/digests |
| 59 | + digest="${{ steps.build.outputs.digest }}" |
| 60 | + touch "${{ runner.temp }}/digests/${digest#sha256:}" |
| 61 | +
|
| 62 | + - name: Upload digest |
| 63 | + uses: actions/upload-artifact@v4 |
| 64 | + with: |
| 65 | + name: digests-${{ env.PLATFORM_PAIR }} |
| 66 | + path: ${{ runner.temp }}/digests/* |
| 67 | + if-no-files-found: error |
| 68 | + retention-days: 1 |
| 69 | + |
| 70 | + merge: |
| 71 | + runs-on: ubuntu-latest |
| 72 | + needs: |
| 73 | + - build |
| 74 | + steps: |
| 75 | + - name: Download digests |
| 76 | + uses: actions/download-artifact@v4 |
| 77 | + with: |
| 78 | + path: ${{ runner.temp }}/digests |
| 79 | + pattern: digests-* |
| 80 | + merge-multiple: true |
| 81 | + |
| 82 | + - name: Login to GHCR |
| 83 | + uses: docker/login-action@v3 |
| 84 | + with: |
| 85 | + registry: ghcr.io |
| 86 | + username: ${{ github.repository_owner }} |
| 87 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 88 | + |
| 89 | + - name: Set up Docker Buildx |
| 90 | + uses: docker/setup-buildx-action@v3 |
| 91 | + |
| 92 | + - name: Docker meta |
| 93 | + id: meta |
| 94 | + uses: docker/metadata-action@v5 |
| 95 | + with: |
| 96 | + images: | |
| 97 | + ${{ env.GITHUB_CR_REPO }} |
| 98 | + tags: | |
| 99 | + type=ref,event=branch |
| 100 | + type=ref,event=pr |
| 101 | + type=semver,pattern={{version}} |
| 102 | + type=semver,pattern={{major}} |
| 103 | +
|
| 104 | + - name: Create manifest list and push |
| 105 | + working-directory: ${{ runner.temp }}/digests |
| 106 | + run: | |
| 107 | + docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ |
| 108 | + $(printf '${{ env.GITHUB_CR_REPO }}@sha256:%s ' *) |
| 109 | +
|
| 110 | + - name: Inspect image |
| 111 | + run: | |
| 112 | + docker buildx imagetools inspect ${{ env.GITHUB_CR_REPO }}:${{ steps.meta.outputs.version }} |
0 commit comments