|
1 | 1 | # https://docs.github.com/en/packages/managing-github-packages-using-github-actions-workflows/publishing-and-installing-a-package-with-github-actions#upgrading-a-workflow-that-accesses-ghcrio |
2 | 2 |
|
3 | | -# This workflow uses actions that are not certified by GitHub. |
4 | | -# They are provided by a third-party and are governed by |
5 | | -# separate terms of service, privacy policy, and support |
6 | | -# documentation. |
7 | | - |
8 | | -# GitHub recommends pinning actions to a commit SHA. |
9 | | -# To get a newer version, you will need to update the SHA. |
10 | | -# You can also reference a tag or branch, but the action may change without warning. |
| 3 | +# Third-party actions are pinned to a full commit SHA (with the human-readable |
| 4 | +# version in a trailing comment) to protect against tag-rewrite supply-chain |
| 5 | +# attacks. To bump a version, update both the SHA and the comment. |
11 | 6 |
|
12 | 7 | name: Create and publish a Docker image |
13 | 8 |
|
14 | 9 | on: |
15 | 10 | release: |
16 | 11 | types: [published] |
17 | | - push: |
18 | | - branches: |
19 | | - - main |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 15 | + cancel-in-progress: false |
20 | 16 |
|
21 | 17 | env: |
22 | 18 | REGISTRY: ghcr.io |
23 | 19 | IMAGE_NAME: ${{ github.repository }} |
24 | 20 |
|
25 | 21 | jobs: |
26 | | - build-and-push-image: |
27 | | - runs-on: ubuntu-latest |
| 22 | + build: |
| 23 | + runs-on: ${{ matrix.runner }} |
28 | 24 | permissions: |
29 | 25 | contents: read |
30 | 26 | packages: write |
| 27 | + strategy: |
| 28 | + fail-fast: false |
| 29 | + matrix: |
| 30 | + include: |
| 31 | + - platform: linux/amd64 |
| 32 | + runner: ubuntu-latest |
| 33 | + suffix: amd64 |
| 34 | + - platform: linux/arm64 |
| 35 | + runner: ubuntu-24.04-arm |
| 36 | + suffix: arm64 |
31 | 37 |
|
32 | 38 | steps: |
33 | 39 | - name: Checkout repository |
34 | | - uses: actions/checkout@v5 |
| 40 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 |
| 41 | + |
| 42 | + - name: Set up Docker Buildx |
| 43 | + uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4 |
| 44 | + |
35 | 45 | - name: Log in to the Container registry |
36 | | - uses: docker/login-action@v3 |
| 46 | + uses: docker/login-action@b45d80f862d83dbcd57f89517bcf500b2ab88fb2 # v4 |
37 | 47 | with: |
38 | 48 | registry: ${{ env.REGISTRY }} |
39 | 49 | username: ${{ github.actor }} |
40 | 50 | password: ${{ secrets.GITHUB_TOKEN }} |
41 | 51 |
|
42 | 52 | - name: Extract metadata (tags, labels) for Docker |
43 | 53 | id: meta |
44 | | - uses: docker/metadata-action@v5 |
| 54 | + uses: docker/metadata-action@030e881283bb7a6894de51c315a6bfe6a94e05cf # v6 |
45 | 55 | with: |
46 | 56 | images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
47 | 57 |
|
48 | | - - name: Build and push Docker image |
49 | | - uses: docker/build-push-action@v6 |
| 58 | + - name: Build and push Docker image by digest |
| 59 | + id: build |
| 60 | + uses: docker/build-push-action@bcafcacb16a39f128d818304e6c9c0c18556b85f # v7 |
50 | 61 | with: |
| 62 | + platforms: ${{ matrix.platform }} |
51 | 63 | target: production |
52 | | - push: true |
53 | | - tags: ${{ steps.meta.outputs.tags }} |
54 | 64 | labels: ${{ steps.meta.outputs.labels }} |
| 65 | + outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true |
| 66 | + cache-from: type=gha,scope=${{ matrix.suffix }} |
| 67 | + cache-to: type=gha,scope=${{ matrix.suffix }},mode=max |
| 68 | + |
| 69 | + - name: Export digest |
| 70 | + env: |
| 71 | + DIGEST: ${{ steps.build.outputs.digest }} |
| 72 | + run: | |
| 73 | + mkdir -p /tmp/digests |
| 74 | + touch "/tmp/digests/${DIGEST#sha256:}" |
| 75 | +
|
| 76 | + - name: Upload digest artifact |
| 77 | + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 |
| 78 | + with: |
| 79 | + name: digests-${{ matrix.suffix }} |
| 80 | + path: /tmp/digests/* |
| 81 | + if-no-files-found: error |
| 82 | + retention-days: 1 |
| 83 | + |
| 84 | + merge: |
| 85 | + runs-on: ubuntu-latest |
| 86 | + needs: build |
| 87 | + permissions: |
| 88 | + contents: read |
| 89 | + packages: write |
| 90 | + |
| 91 | + steps: |
| 92 | + - name: Download digests |
| 93 | + uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0 |
| 94 | + with: |
| 95 | + path: /tmp/digests |
| 96 | + pattern: digests-* |
| 97 | + merge-multiple: true |
| 98 | + |
| 99 | + - name: Set up Docker Buildx |
| 100 | + uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4 |
| 101 | + |
| 102 | + - name: Log in to the Container registry |
| 103 | + uses: docker/login-action@b45d80f862d83dbcd57f89517bcf500b2ab88fb2 # v4 |
| 104 | + with: |
| 105 | + registry: ${{ env.REGISTRY }} |
| 106 | + username: ${{ github.actor }} |
| 107 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 108 | + |
| 109 | + - name: Extract metadata (tags, labels) for Docker |
| 110 | + id: meta |
| 111 | + uses: docker/metadata-action@030e881283bb7a6894de51c315a6bfe6a94e05cf # v6 |
| 112 | + with: |
| 113 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 114 | + |
| 115 | + - name: Create and push multi-arch manifest |
| 116 | + working-directory: /tmp/digests |
| 117 | + env: |
| 118 | + IMAGE_REF: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 119 | + run: | |
| 120 | + docker buildx imagetools create \ |
| 121 | + $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ |
| 122 | + $(printf "${IMAGE_REF}@sha256:%s " *) |
| 123 | +
|
| 124 | + - name: Inspect image |
| 125 | + env: |
| 126 | + IMAGE_REF: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 127 | + VERSION: ${{ steps.meta.outputs.version }} |
| 128 | + run: | |
| 129 | + docker buildx imagetools inspect "${IMAGE_REF}:${VERSION}" |
0 commit comments