|
| 1 | +name: Build and push docker image |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + push: |
| 6 | + branches: [main] |
| 7 | + |
| 8 | +env: |
| 9 | + CARGO_TERM_COLOR: always |
| 10 | + REGISTRY_IMAGE: ghcr.io/svix/openapi-codegen |
| 11 | + |
| 12 | +jobs: |
| 13 | + build: |
| 14 | + strategy: |
| 15 | + matrix: |
| 16 | + platform: [ {runner: ubuntu-24.04, name: amd64 }, {runner: ubuntu-24.04-arm, name: arm64 } ] |
| 17 | + name: Build and publish ${{ matrix.platform.name }} docker image |
| 18 | + if: github.ref == 'refs/heads/main' |
| 19 | + runs-on: "${{ matrix.platform.runner }}" |
| 20 | + steps: |
| 21 | + - uses: actions/checkout@v4 |
| 22 | + |
| 23 | + - name: Login to ghcr |
| 24 | + uses: docker/login-action@v3 |
| 25 | + with: |
| 26 | + registry: ghcr.io |
| 27 | + username: ${{ github.actor }} |
| 28 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 29 | + |
| 30 | + - name: Set up Docker Buildx |
| 31 | + uses: docker/setup-buildx-action@v3 |
| 32 | + |
| 33 | + - name: Build and push by digest |
| 34 | + id: build |
| 35 | + uses: docker/build-push-action@v6 |
| 36 | + with: |
| 37 | + tags: ${{ env.REGISTRY_IMAGE }} |
| 38 | + file: Dockerfile.${{ matrix.platform.name }} |
| 39 | + cache-from: type=gha |
| 40 | + cache-to: type=gha |
| 41 | + platforms: linux/${{ matrix.platform.name }} |
| 42 | + outputs: type=image,push-by-digest=true,name-canonical=true,push=true |
| 43 | + |
| 44 | + - name: Export digest |
| 45 | + # we create empty files with the sha256 digest of the docker image as the filename |
| 46 | + # since we did not push with a tag, the only way to identify the image is with the digest |
| 47 | + run: | |
| 48 | + mkdir -p ${{ runner.temp }}/digests |
| 49 | + digest="${{ steps.build.outputs.digest }}" |
| 50 | + touch "${{ runner.temp }}/digests/${digest#sha256:}" |
| 51 | +
|
| 52 | + - name: Upload digest |
| 53 | + uses: actions/upload-artifact@v4 |
| 54 | + with: |
| 55 | + name: digests-${{ matrix.platform.name }} |
| 56 | + path: ${{ runner.temp }}/digests/* |
| 57 | + if-no-files-found: error |
| 58 | + retention-days: 1 |
| 59 | + |
| 60 | + publish-merged-manifest: |
| 61 | + if: github.ref == 'refs/heads/main' |
| 62 | + runs-on: ubuntu-24.04 |
| 63 | + needs: |
| 64 | + - build |
| 65 | + steps: |
| 66 | + - uses: actions/checkout@v4 |
| 67 | + |
| 68 | + - name: Download digests |
| 69 | + uses: actions/download-artifact@v4 |
| 70 | + with: |
| 71 | + path: ${{ runner.temp }}/digests |
| 72 | + pattern: digests-* |
| 73 | + merge-multiple: true |
| 74 | + |
| 75 | + - name: Login to ghcr |
| 76 | + uses: docker/login-action@v3 |
| 77 | + with: |
| 78 | + registry: ghcr.io |
| 79 | + username: ${{ github.actor }} |
| 80 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 81 | + |
| 82 | + - name: Set up Docker Buildx |
| 83 | + uses: docker/setup-buildx-action@v3 |
| 84 | + |
| 85 | + - run: echo "IMAGE_TAG=$(date +%Y%m%d)-$(git rev-parse --short "$GITHUB_SHA")" >> "$GITHUB_ENV" |
| 86 | + |
| 87 | + - name: Create manifest list and push |
| 88 | + # inside the ${{ runner.temp }}/digests we downloaded empty files with the sha256 digest of the image as the filename |
| 89 | + # using printf we get the digest from the filename and we add the digest to the manifest |
| 90 | + # this is the recommend way of doing things :( |
| 91 | + # https://docs.docker.com/build/ci/github-actions/multi-platform/#distribute-build-across-multiple-runners |
| 92 | + working-directory: ${{ runner.temp }}/digests |
| 93 | + run: | |
| 94 | + docker buildx imagetools create \ |
| 95 | + -t ${{ env.REGISTRY_IMAGE }}:latest \ |
| 96 | + -t ${{ env.REGISTRY_IMAGE }}:${{ env.IMAGE_TAG }} \ |
| 97 | + $(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *) |
| 98 | +
|
| 99 | + - name: Inspect image |
| 100 | + run: | |
| 101 | + docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:latest |
0 commit comments