|
| 1 | +name: Build and Publish Docker Image |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + target: |
| 7 | + type: choice |
| 8 | + description: Which image to release |
| 9 | + required: true |
| 10 | + options: |
| 11 | + - riscv-unknown-elf-toolchain |
| 12 | + - rust-riscv32imac-cross |
| 13 | + - rust-riscv64imac-cross |
| 14 | + push: |
| 15 | + tags: |
| 16 | + # matches tags like `service/v1.0.0` |
| 17 | + - "*/v*" |
| 18 | + |
| 19 | +env: |
| 20 | + REGISTRY: ghcr.io |
| 21 | + REGISTRY_IMAGE: ghcr.io/commonwarexyz/monorepo |
| 22 | + GIT_REF_NAME: ${{ github.ref_name }} |
| 23 | + |
| 24 | +jobs: |
| 25 | + prepare: |
| 26 | + name: Prepare Bake |
| 27 | + runs-on: ubuntu-latest |
| 28 | + permissions: |
| 29 | + packages: write |
| 30 | + outputs: |
| 31 | + matrix: ${{ steps.platforms.outputs.matrix }} |
| 32 | + target: ${{ steps.target-spec.outputs.target }} |
| 33 | + steps: |
| 34 | + - name: Checkout |
| 35 | + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 36 | + - name: Specify Target |
| 37 | + id: target-spec |
| 38 | + run: | |
| 39 | + export TARGET="${{ inputs.target }}" |
| 40 | + if [[ -z $TARGET ]]; then |
| 41 | + export TARGET="${GIT_REF_NAME%/*}" |
| 42 | + fi |
| 43 | + echo "Target: $TARGET" |
| 44 | + echo "target=$TARGET" >> $GITHUB_OUTPUT |
| 45 | + - name: Create matrix |
| 46 | + id: platforms |
| 47 | + run: | |
| 48 | + echo "matrix=$(docker buildx bake -f docker/docker-bake.hcl ${{ steps.target-spec.outputs.target }} --print | jq -cr '.target."${{ steps.target-spec.outputs.target }}".platforms')" >> ${GITHUB_OUTPUT} |
| 49 | + - name: Show matrix |
| 50 | + run: | |
| 51 | + echo ${{ steps.platforms.outputs.matrix }} |
| 52 | + - name: Docker meta |
| 53 | + id: meta |
| 54 | + uses: docker/metadata-action@c1e51972afc2121e065aed6d45c65596fe445f3f # v5.8.0 |
| 55 | + with: |
| 56 | + images: ${{ env.REGISTRY_IMAGE }}/${{ steps.target-spec.outputs.target }} |
| 57 | + tags: | |
| 58 | + type=ref,event=branch |
| 59 | + type=match,pattern=v(.*),group=1,event=tag |
| 60 | + type=ref,event=pr |
| 61 | + - name: Rename meta bake definition file |
| 62 | + run: | |
| 63 | + mv "${{ steps.meta.outputs.bake-file }}" "${{ runner.temp }}/bake-meta.json" |
| 64 | + - name: Upload meta bake definition |
| 65 | + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 |
| 66 | + with: |
| 67 | + name: bake-meta |
| 68 | + path: ${{ runner.temp }}/bake-meta.json |
| 69 | + if-no-files-found: error |
| 70 | + retention-days: 1 |
| 71 | + |
| 72 | + build: |
| 73 | + name: Build Image (${{ needs.prepare.outputs.target }} - ${{ matrix.platform }}) |
| 74 | + runs-on: ${{ matrix.platform == 'linux/amd64' && 'ubuntu-latest' || 'ubuntu-22.04-arm' }} |
| 75 | + permissions: |
| 76 | + packages: write |
| 77 | + needs: |
| 78 | + - prepare |
| 79 | + strategy: |
| 80 | + fail-fast: false |
| 81 | + matrix: |
| 82 | + platform: ${{ fromJson(needs.prepare.outputs.matrix) }} |
| 83 | + steps: |
| 84 | + - name: Prepare |
| 85 | + run: | |
| 86 | + platform=${{ matrix.platform }} |
| 87 | + echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV |
| 88 | + - name: Download meta bake definition |
| 89 | + uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5.0.0 |
| 90 | + with: |
| 91 | + name: bake-meta |
| 92 | + path: ${{ runner.temp }} |
| 93 | + - name: Authenticate with container registry |
| 94 | + uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3.6.0 |
| 95 | + with: |
| 96 | + registry: ${{ env.REGISTRY }} |
| 97 | + username: ${{ github.actor }} |
| 98 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 99 | + - name: Set up Docker Buildx |
| 100 | + uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3.11.1 |
| 101 | + - name: Build |
| 102 | + id: bake |
| 103 | + uses: docker/bake-action@3acf805d94d93a86cce4ca44798a76464a75b88c # v6.9.0 |
| 104 | + with: |
| 105 | + files: | |
| 106 | + ./docker/docker-bake.hcl |
| 107 | + cwd://${{ runner.temp }}/bake-meta.json |
| 108 | + targets: ${{ needs.prepare.outputs.target }} |
| 109 | + set: | |
| 110 | + *.tags= |
| 111 | + *.platform=${{ matrix.platform }} |
| 112 | + *.output=type=image,"name=${{ env.REGISTRY_IMAGE }}/${{ needs.prepare.outputs.target }}",push-by-digest=true,name-canonical=true,push=true |
| 113 | + - name: Export digest |
| 114 | + run: | |
| 115 | + mkdir -p ${{ runner.temp }}/digests |
| 116 | + digest="${{ fromJSON(steps.bake.outputs.metadata)[needs.prepare.outputs.target]['containerimage.digest'] }}" |
| 117 | + touch "${{ runner.temp }}/digests/${digest#sha256:}" |
| 118 | + - name: Upload digest |
| 119 | + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 |
| 120 | + with: |
| 121 | + name: digests-${{ env.PLATFORM_PAIR }} |
| 122 | + path: ${{ runner.temp }}/digests/* |
| 123 | + if-no-files-found: error |
| 124 | + retention-days: 1 |
| 125 | + |
| 126 | + merge: |
| 127 | + name: Publish Manifest (${{ needs.prepare.outputs.target }}) |
| 128 | + runs-on: ubuntu-latest |
| 129 | + permissions: |
| 130 | + packages: write |
| 131 | + needs: |
| 132 | + - build |
| 133 | + - prepare |
| 134 | + steps: |
| 135 | + - name: Download meta bake definition |
| 136 | + uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5.0.0 |
| 137 | + with: |
| 138 | + name: bake-meta |
| 139 | + path: ${{ runner.temp }} |
| 140 | + - name: Download digests |
| 141 | + uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5.0.0 |
| 142 | + with: |
| 143 | + path: ${{ runner.temp }}/digests |
| 144 | + pattern: digests-* |
| 145 | + merge-multiple: true |
| 146 | + - name: Authenticate with container registry |
| 147 | + uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3.6.0 |
| 148 | + with: |
| 149 | + registry: ${{ env.REGISTRY }} |
| 150 | + username: ${{ github.actor }} |
| 151 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 152 | + - name: Set up Docker Buildx |
| 153 | + uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3.11.1 |
| 154 | + - name: Create manifest list and push |
| 155 | + working-directory: ${{ runner.temp }}/digests |
| 156 | + run: | |
| 157 | + docker buildx imagetools create $(jq -cr '.target."docker-metadata-action".tags | map(select(startswith("${{ env.REGISTRY_IMAGE }}/${{ needs.prepare.outputs.target }}")) | "-t " + .) | join(" ")' ${{ runner.temp }}/bake-meta.json) \ |
| 158 | + $(printf '${{ env.REGISTRY_IMAGE }}/${{ needs.prepare.outputs.target }}@sha256:%s ' *) |
| 159 | + - name: Inspect image |
| 160 | + run: | |
| 161 | + docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}/${{ needs.prepare.outputs.target }}:$(jq -r '.target."docker-metadata-action".args.DOCKER_META_VERSION' ${{ runner.temp }}/bake-meta.json) |
0 commit comments