|
| 1 | +name: container |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + workflow_run: |
| 6 | + workflows: [Rust] |
| 7 | + types: [completed] |
| 8 | + branches: [main] |
| 9 | + push: |
| 10 | + tags: ['v*'] |
| 11 | + |
| 12 | +permissions: |
| 13 | + contents: read |
| 14 | + packages: write |
| 15 | + |
| 16 | +concurrency: |
| 17 | + group: ${{ github.workflow }}-${{ github.event_name == 'workflow_run' && github.event.workflow_run.head_branch || github.head_ref || github.ref }} |
| 18 | + cancel-in-progress: true |
| 19 | + |
| 20 | +env: |
| 21 | + REGISTRY: ghcr.io |
| 22 | + IMAGE_NAME: ${{ github.repository }} |
| 23 | + |
| 24 | +jobs: |
| 25 | + build: |
| 26 | + if: | |
| 27 | + github.event_name == 'pull_request' || |
| 28 | + github.event_name == 'push' || |
| 29 | + (github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success') |
| 30 | + runs-on: ${{ matrix.runner }} |
| 31 | + timeout-minutes: 45 |
| 32 | + strategy: |
| 33 | + fail-fast: false |
| 34 | + matrix: |
| 35 | + include: |
| 36 | + - runner: ubuntu-latest |
| 37 | + platform: linux/amd64 |
| 38 | + arch: amd64 |
| 39 | + - runner: ubuntu-24.04-arm |
| 40 | + platform: linux/arm64 |
| 41 | + arch: arm64 |
| 42 | + steps: |
| 43 | + - name: Checkout |
| 44 | + uses: actions/checkout@v4 |
| 45 | + with: |
| 46 | + ref: ${{ github.event.workflow_run.head_sha || '' }} |
| 47 | + |
| 48 | + - name: Set up Docker Buildx |
| 49 | + uses: docker/setup-buildx-action@v3 |
| 50 | + |
| 51 | + - name: Log in to GHCR |
| 52 | + if: github.event_name != 'pull_request' |
| 53 | + uses: docker/login-action@v3 |
| 54 | + with: |
| 55 | + registry: ${{ env.REGISTRY }} |
| 56 | + username: ${{ github.actor }} |
| 57 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 58 | + |
| 59 | + - name: Extract Docker metadata |
| 60 | + id: meta |
| 61 | + uses: docker/metadata-action@v5 |
| 62 | + with: |
| 63 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 64 | + |
| 65 | + - name: Build and push by digest |
| 66 | + id: build |
| 67 | + uses: docker/build-push-action@v6 |
| 68 | + with: |
| 69 | + context: . |
| 70 | + platforms: ${{ matrix.platform }} |
| 71 | + labels: ${{ steps.meta.outputs.labels }} |
| 72 | + outputs: >- |
| 73 | + type=image, |
| 74 | + name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}, |
| 75 | + push-by-digest=true, |
| 76 | + name-canonical=true, |
| 77 | + push=${{ github.event_name != 'pull_request' }} |
| 78 | + cache-from: type=gha,scope=${{ matrix.arch }} |
| 79 | + cache-to: type=gha,scope=${{ matrix.arch }},mode=max |
| 80 | + |
| 81 | + - name: Export digest |
| 82 | + if: github.event_name != 'pull_request' |
| 83 | + run: | |
| 84 | + mkdir -p /tmp/digests |
| 85 | + digest="${{ steps.build.outputs.digest }}" |
| 86 | + touch "/tmp/digests/${digest#sha256:}" |
| 87 | +
|
| 88 | + - name: Upload digest |
| 89 | + if: github.event_name != 'pull_request' |
| 90 | + uses: actions/upload-artifact@v4 |
| 91 | + with: |
| 92 | + name: digests-${{ matrix.arch }} |
| 93 | + path: /tmp/digests/* |
| 94 | + if-no-files-found: error |
| 95 | + retention-days: 1 |
| 96 | + |
| 97 | + merge: |
| 98 | + runs-on: ubuntu-latest |
| 99 | + timeout-minutes: 10 |
| 100 | + if: github.event_name != 'pull_request' |
| 101 | + needs: build |
| 102 | + steps: |
| 103 | + - name: Download digests |
| 104 | + uses: actions/download-artifact@v4 |
| 105 | + with: |
| 106 | + path: /tmp/digests |
| 107 | + pattern: digests-* |
| 108 | + merge-multiple: true |
| 109 | + |
| 110 | + - name: Set up Docker Buildx |
| 111 | + uses: docker/setup-buildx-action@v3 |
| 112 | + |
| 113 | + - name: Log in to GHCR |
| 114 | + uses: docker/login-action@v3 |
| 115 | + with: |
| 116 | + registry: ${{ env.REGISTRY }} |
| 117 | + username: ${{ github.actor }} |
| 118 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 119 | + |
| 120 | + - name: Extract Docker metadata |
| 121 | + id: meta |
| 122 | + uses: docker/metadata-action@v5 |
| 123 | + with: |
| 124 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 125 | + tags: | |
| 126 | + type=edge,branch=main |
| 127 | + type=sha,prefix=sha- |
| 128 | + type=semver,pattern={{version}} |
| 129 | + type=semver,pattern={{major}}.{{minor}} |
| 130 | + type=semver,pattern={{major}} |
| 131 | +
|
| 132 | + - name: Create manifest list and push |
| 133 | + working-directory: /tmp/digests |
| 134 | + run: | |
| 135 | + docker buildx imagetools create \ |
| 136 | + $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ |
| 137 | + $(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *) |
| 138 | +
|
| 139 | + - name: Inspect image |
| 140 | + run: | |
| 141 | + docker buildx imagetools inspect \ |
| 142 | + $(jq -cr '.tags[0]' <<< "$DOCKER_METADATA_OUTPUT_JSON") |
0 commit comments