|
1 | | -name: Upload Docker images to ghcr.io |
| 1 | +# |
| 2 | +# Based on: |
| 3 | +# |
| 4 | +# Docker docs: Distribute build across multiple runners |
| 5 | +# https://docs.docker.com/build/ci/github-actions/multi-platform/#distribute-build-across-multiple-runners |
| 6 | +# |
| 7 | + |
| 8 | +name: Upload Docker images to GitHub Container Registry (ghcr.io) |
| 9 | + |
2 | 10 | on: |
3 | 11 | release: |
4 | | - types: [created] |
| 12 | + types: |
| 13 | + - released |
| 14 | + |
| 15 | + push: |
| 16 | + branches: |
| 17 | + - master |
| 18 | + - main |
| 19 | + |
| 20 | + pull_request: |
| 21 | + branches: |
| 22 | + - master |
| 23 | + - main |
| 24 | + |
| 25 | + workflow_dispatch: |
| 26 | + inputs: |
| 27 | + ref: |
| 28 | + description: Git tag to push the image |
| 29 | + required: true |
| 30 | + type: string |
| 31 | + |
5 | 32 | jobs: |
6 | | - docker: |
| 33 | + prepare: |
| 34 | + name: Prepare |
| 35 | + runs-on: ubuntu-latest |
| 36 | + outputs: |
| 37 | + github_repository: ${{ steps.vars.outputs.github_repository }} |
| 38 | + publish_image: ${{ steps.vars.outputs.publish_image }} |
| 39 | + semver_value: ${{ steps.vars.outputs.semver_value }} |
| 40 | + steps: |
| 41 | + - id: vars |
| 42 | + name: Prepare outputs |
| 43 | + run: | |
| 44 | + function prepend() { while read line; do echo "${1}${line}"; done; } |
| 45 | + readonly NOTICE_VAR='::notice title=Setting variable::' |
| 46 | +
|
| 47 | + github_repository=${{ github.repository }} |
| 48 | + echo "github_repository=${github_repository,,}" | tee -a $GITHUB_OUTPUT | prepend "$NOTICE_VAR" |
| 49 | +
|
| 50 | + if [ "${{ github.event_name }}" = "release" ]; then |
| 51 | + echo "publish_image=true" | tee -a $GITHUB_OUTPUT | prepend "$NOTICE_VAR" |
| 52 | + elif [ "${{ github.event_name }}" = "workflow_dispatch" ]; then |
| 53 | + echo "publish_image=true" | tee -a $GITHUB_OUTPUT | prepend "$NOTICE_VAR" |
| 54 | + echo "semver_value=,value=${{ inputs.ref }}" | tee -a $GITHUB_OUTPUT | prepend "$NOTICE_VAR" |
| 55 | + fi |
| 56 | +
|
| 57 | + build: |
7 | 58 | name: Build image |
| 59 | + if: github.event_name == 'pull_request' |
8 | 60 | runs-on: ubuntu-latest |
| 61 | + needs: |
| 62 | + - prepare |
9 | 63 | steps: |
10 | 64 | - name: Check out code |
11 | | - uses: actions/checkout@v4 |
| 65 | + uses: actions/checkout@v5 |
| 66 | + with: |
| 67 | + fetch-tags: true |
| 68 | + ref: ${{ github.ref }} |
| 69 | + |
12 | 70 | - name: Docker meta |
13 | 71 | id: meta |
14 | 72 | uses: docker/metadata-action@v5 |
15 | 73 | with: |
16 | 74 | images: ghcr.io/${{ github.repository }} |
17 | | - # create latest tag for branch events |
18 | | - flavor: | |
19 | | - latest=auto |
20 | | - tags: | |
21 | | - type=ref,event=branch |
22 | | - type=ref,event=pr |
23 | | - type=semver,pattern={{version}} |
24 | | - type=semver,pattern={{major}}.{{minor}} |
25 | | - type=semver,pattern={{major}}.{{minor}}.{{patch}} |
26 | | - - name: Login to ghcr.io |
| 75 | + |
| 76 | + - name: Set up Docker Buildx |
| 77 | + uses: docker/setup-buildx-action@v3 |
| 78 | + |
| 79 | + - name: Build |
| 80 | + uses: docker/build-push-action@v6 |
| 81 | + with: |
| 82 | + context: . |
| 83 | + platforms: linux/amd64 |
| 84 | + push: false |
| 85 | + tags: ${{ steps.meta.outputs.tags }} |
| 86 | + labels: ${{ steps.meta.outputs.labels }} |
| 87 | + annotations: ${{ steps.meta.outputs.annotations }} |
| 88 | + |
| 89 | + mbuild: |
| 90 | + name: Build image |
| 91 | + needs: |
| 92 | + - prepare |
| 93 | + if: github.event_name != 'pull_request' |
| 94 | + runs-on: ubuntu-latest |
| 95 | + strategy: |
| 96 | + fail-fast: false |
| 97 | + matrix: |
| 98 | + platform: |
| 99 | + - platform: linux/amd64 |
| 100 | + - platform: linux/arm64 |
| 101 | + qemu: arm64 |
| 102 | + - platform: linux/arm/v7 |
| 103 | + qemu: arm |
| 104 | + - platform: linux/arm/v6 |
| 105 | + qemu: arm |
| 106 | + steps: |
| 107 | + - name: Prepare |
| 108 | + id: prepare |
| 109 | + run: | |
| 110 | + platform=${{ matrix.platform.platform }} |
| 111 | + echo "platform_pair=${platform//\//-}" | tee -a $GITHUB_OUTPUT |
| 112 | +
|
| 113 | + - name: Check out code |
| 114 | + if: github.event_name == 'workflow_dispatch' |
| 115 | + uses: actions/checkout@v5 |
| 116 | + with: |
| 117 | + ref: ${{ inputs.ref }} |
| 118 | + fetch-tags: true |
| 119 | + |
| 120 | + - name: Check out code |
| 121 | + if: github.event_name != 'workflow_dispatch' |
| 122 | + uses: actions/checkout@v5 |
| 123 | + with: |
| 124 | + fetch-tags: true |
| 125 | + ref: ${{ github.ref }} |
| 126 | + |
| 127 | + - name: Docker meta |
| 128 | + id: meta |
| 129 | + uses: docker/metadata-action@v5 |
| 130 | + with: |
| 131 | + images: ghcr.io/${{ github.repository }} |
| 132 | + |
| 133 | + - name: Set up QEMU |
| 134 | + uses: docker/setup-qemu-action@v3 |
| 135 | + if: ${{ matrix.platform.qemu }} |
| 136 | + with: |
| 137 | + platforms: ${{ matrix.platform.qemu }} |
| 138 | + cache-image: false |
| 139 | + |
| 140 | + - name: Set up Docker Buildx |
| 141 | + uses: docker/setup-buildx-action@v3 |
| 142 | + |
| 143 | + - name: Login to GitHub Container Registry |
| 144 | + if: needs.prepare.outputs.publish_image |
27 | 145 | uses: docker/login-action@v3 |
28 | 146 | with: |
29 | 147 | registry: ghcr.io |
30 | 148 | username: ${{ github.actor }} |
31 | 149 | password: ${{ secrets.GITHUB_TOKEN }} |
32 | | - - name: Build and push |
33 | | - id: docker_build |
34 | | - uses: docker/build-push-action@v5 |
| 150 | + |
| 151 | + - name: Build |
| 152 | + if: ${{ ! needs.prepare.outputs.publish_image }} |
| 153 | + uses: docker/build-push-action@v6 |
35 | 154 | with: |
36 | | - # push for non-pr events |
37 | | - push: ${{ github.event_name != 'pull_request' }} |
38 | 155 | context: . |
| 156 | + platforms: ${{ matrix.platform.platform }} |
| 157 | + push: false |
39 | 158 | tags: ${{ steps.meta.outputs.tags }} |
40 | 159 | labels: ${{ steps.meta.outputs.labels }} |
| 160 | + annotations: ${{ steps.meta.outputs.annotations }} |
| 161 | + |
| 162 | + - name: Build and push |
| 163 | + if: needs.prepare.outputs.publish_image |
| 164 | + uses: docker/build-push-action@v6 |
| 165 | + id: build |
| 166 | + with: |
| 167 | + context: . |
| 168 | + platforms: ${{ matrix.platform.platform }} |
| 169 | + labels: ${{ steps.meta.outputs.labels }} |
| 170 | + annotations: ${{ steps.meta.outputs.annotations }} |
| 171 | + outputs: type=image,"name=ghcr.io/${{ needs.prepare.outputs.github_repository }}",push-by-digest=true,name-canonical=true,push=true |
| 172 | + |
| 173 | + - name: Export digest |
| 174 | + if: needs.prepare.outputs.publish_image |
| 175 | + run: | |
| 176 | + mkdir -p ${{ runner.temp }}/digests |
| 177 | + digest='${{ steps.build.outputs.digest }}' |
| 178 | + touch "${{ runner.temp }}/digests/${digest#sha256:}" |
| 179 | +
|
| 180 | + - name: Upload digest |
| 181 | + if: needs.prepare.outputs.publish_image |
| 182 | + uses: actions/upload-artifact@v4 |
| 183 | + with: |
| 184 | + name: digests-${{ steps.prepare.outputs.platform_pair }} |
| 185 | + path: ${{ runner.temp }}/digests/* |
| 186 | + if-no-files-found: error |
| 187 | + retention-days: 1 |
| 188 | + compression-level: 0 |
| 189 | + |
| 190 | + merge: |
| 191 | + name: Merge images |
| 192 | + runs-on: ubuntu-latest |
| 193 | + needs: |
| 194 | + - prepare |
| 195 | + - mbuild |
| 196 | + if: needs.prepare.outputs.publish_image |
| 197 | + steps: |
| 198 | + - name: Download digests |
| 199 | + uses: actions/download-artifact@v5 |
| 200 | + with: |
| 201 | + path: ${{ runner.temp }}/digests |
| 202 | + pattern: digests-* |
| 203 | + merge-multiple: true |
| 204 | + |
| 205 | + - name: Login to GHCR |
| 206 | + uses: docker/login-action@v3 |
| 207 | + with: |
| 208 | + registry: ghcr.io |
| 209 | + username: ${{ github.actor }} |
| 210 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 211 | + |
| 212 | + - name: Set up Docker Buildx |
| 213 | + uses: docker/setup-buildx-action@v3 |
| 214 | + |
| 215 | + - name: Docker meta |
| 216 | + id: meta |
| 217 | + uses: docker/metadata-action@v5 |
| 218 | + with: |
| 219 | + images: ghcr.io/${{ github.repository }} |
| 220 | + flavor: | |
| 221 | + latest=${{ github.event_name == 'workflow_dispatch' && 'false' || 'auto' }} |
| 222 | + tags: | |
| 223 | + type=semver,pattern={{version}}${{ needs.prepare.outputs.semver_value }} |
| 224 | + type=semver,pattern={{major}}.{{minor}}${{ needs.prepare.outputs.semver_value }} |
| 225 | + type=semver,pattern={{major}}.{{minor}}.{{patch}}${{ needs.prepare.outputs.semver_value }} |
| 226 | +
|
| 227 | + - name: Create manifest list and push |
| 228 | + working-directory: ${{ runner.temp }}/digests |
| 229 | + run: | |
| 230 | + docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ |
| 231 | + $(printf 'ghcr.io/${{ needs.prepare.outputs.github_repository }}@sha256:%s ' *) |
| 232 | +
|
| 233 | + - name: Inspect image |
| 234 | + run: docker buildx imagetools inspect ghcr.io/${{ needs.prepare.outputs.github_repository }}:${{ steps.meta.outputs.version }} |
0 commit comments