|
| 1 | +# Based on |
| 2 | +# <https://docs.github.com/en/actions/publishing-packages/publishing-docker-images> |
| 3 | +# <https://docs.docker.com/build/ci/github-actions/multi-platform/#distribute-build-across-multiple-runners>. |
| 4 | + |
| 5 | +name: Publish Docker image |
| 6 | + |
| 7 | +on: |
| 8 | + workflow_dispatch: |
| 9 | + push: |
| 10 | + branches: [ "main" ] |
| 11 | + # Publish semver tags as releases. |
| 12 | + tags: [ 'v*.*.*' ] |
| 13 | + paths: |
| 14 | + - '.github/workflows/DockerPublish.yml' |
| 15 | + - 'Dockerfile' |
| 16 | + - 'Project.toml' |
| 17 | + - 'julia_cpu_target.sh' |
| 18 | + - '**/*.jl' |
| 19 | + pull_request: |
| 20 | + branches: [ "main" ] |
| 21 | + paths: |
| 22 | + - '.github/workflows/DockerPublish.yml' |
| 23 | + - 'Dockerfile' |
| 24 | + - 'Project.toml' |
| 25 | + - 'julia_cpu_target.sh' |
| 26 | + - '**/*.jl' |
| 27 | + |
| 28 | +env: |
| 29 | + REGISTRY: ghcr.io |
| 30 | + IMAGE_NAME: ${{ github.repository }} |
| 31 | + |
| 32 | +concurrency: |
| 33 | + # Skip intermediate builds: always. |
| 34 | + # Cancel intermediate builds: only if it is a pull request build. |
| 35 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 36 | + cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }} |
| 37 | + |
| 38 | +jobs: |
| 39 | + |
| 40 | + build: |
| 41 | + |
| 42 | + strategy: |
| 43 | + fail-fast: false |
| 44 | + matrix: |
| 45 | + include: |
| 46 | + - platform: 'linux/amd64' |
| 47 | + os: 'ubuntu-latest' |
| 48 | + - platform: 'linux/arm64' |
| 49 | + os: 'ubuntu-24.04-arm' |
| 50 | + |
| 51 | + runs-on: ${{ matrix.os }} |
| 52 | + permissions: |
| 53 | + contents: read |
| 54 | + packages: write |
| 55 | + attestations: write |
| 56 | + # This is used to complete the identity challenge |
| 57 | + # with sigstore/fulcio when running outside of PRs. |
| 58 | + id-token: write |
| 59 | + |
| 60 | + steps: |
| 61 | + |
| 62 | + - name: Checkout repository |
| 63 | + uses: actions/checkout@v4 |
| 64 | + |
| 65 | + - name: Prepare |
| 66 | + run: | |
| 67 | + platform=${{ matrix.platform }} |
| 68 | + echo "PLATFORM_PAIR=${platform//\//-}" >> "${GITHUB_ENV}" |
| 69 | +
|
| 70 | + # Docker is terrible and doesn't like uppercase image names. |
| 71 | + - name: Lowercase image name |
| 72 | + run: | |
| 73 | + IMAGE_NAME=$(echo ${IMAGE_NAME} | tr A-Z a-z) |
| 74 | + echo "IMAGE_NAME=${IMAGE_NAME}" >> "${GITHUB_ENV}" |
| 75 | +
|
| 76 | + # Needed to make caching on GHA work: https://stackoverflow.com/a/73884678. |
| 77 | + # https://github.com/docker/setup-buildx-action |
| 78 | + - name: Setup Docker buildx |
| 79 | + uses: docker/setup-buildx-action@v3 |
| 80 | + |
| 81 | + # Log into the registry except on PRs. |
| 82 | + # https://github.com/docker/login-action |
| 83 | + - name: Log into registry ${{ env.REGISTRY }} |
| 84 | + if: github.event_name != 'pull_request' |
| 85 | + uses: docker/login-action@v3 |
| 86 | + with: |
| 87 | + registry: ${{ env.REGISTRY }} |
| 88 | + username: ${{ github.actor }} |
| 89 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 90 | + |
| 91 | + # Extract metadata (tags, labels) for Docker |
| 92 | + # https://github.com/docker/metadata-action |
| 93 | + - name: Extract Docker metadata |
| 94 | + id: meta |
| 95 | + uses: docker/metadata-action@v5 |
| 96 | + with: |
| 97 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 98 | + |
| 99 | + # Build and push Docker image with Buildx (don't push on PR) |
| 100 | + # https://github.com/docker/build-push-action |
| 101 | + - name: Build and push by digest |
| 102 | + id: build |
| 103 | + uses: docker/build-push-action@v6 |
| 104 | + with: |
| 105 | + context: . |
| 106 | + push: ${{ github.event_name != 'pull_request' }} |
| 107 | + platforms: ${{ matrix.platform }} |
| 108 | + labels: ${{ steps.meta.outputs.labels }} |
| 109 | + outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=${{ github.event_name != 'pull_request' }} |
| 110 | + tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 111 | + # See: https://docs.docker.com/build/ci/github-actions/cache/#github-cache |
| 112 | + cache-from: type=gha |
| 113 | + cache-to: type=gha,mode=max |
| 114 | + |
| 115 | + - name: Export digest |
| 116 | + run: | |
| 117 | + mkdir -p /tmp/digests |
| 118 | + digest="${{ steps.build.outputs.digest }}" |
| 119 | + touch "/tmp/digests/${digest#sha256:}" |
| 120 | +
|
| 121 | + - name: Upload digest |
| 122 | + uses: actions/upload-artifact@v4 |
| 123 | + with: |
| 124 | + name: digests-${{ env.PLATFORM_PAIR }} |
| 125 | + path: /tmp/digests/* |
| 126 | + if-no-files-found: error |
| 127 | + retention-days: 1 |
| 128 | + |
| 129 | + merge: |
| 130 | + runs-on: ubuntu-latest |
| 131 | + permissions: |
| 132 | + contents: read |
| 133 | + packages: write |
| 134 | + attestations: write |
| 135 | + # This is used to complete the identity challenge |
| 136 | + # with sigstore/fulcio when running outside of PRs. |
| 137 | + id-token: write |
| 138 | + if: github.event_name != 'pull_request' |
| 139 | + needs: |
| 140 | + - build |
| 141 | + steps: |
| 142 | + |
| 143 | + # Docker is terrible and doesn't like uppercase image names. |
| 144 | + - name: Lowercase image name |
| 145 | + run: | |
| 146 | + IMAGE_NAME=$(echo ${IMAGE_NAME} | tr A-Z a-z) |
| 147 | + echo "IMAGE_NAME=${IMAGE_NAME}" >> "${GITHUB_ENV}" |
| 148 | +
|
| 149 | + - name: Download digests |
| 150 | + uses: actions/download-artifact@v4 |
| 151 | + with: |
| 152 | + path: /tmp/digests |
| 153 | + pattern: digests-* |
| 154 | + merge-multiple: true |
| 155 | + |
| 156 | + # https://github.com/docker/setup-buildx-action |
| 157 | + - name: Set up Docker Buildx |
| 158 | + uses: docker/setup-buildx-action@v3 |
| 159 | + |
| 160 | + # Extract metadata (tags, labels) for Docker |
| 161 | + # https://github.com/docker/metadata-action |
| 162 | + - name: Docker meta |
| 163 | + id: meta |
| 164 | + uses: docker/metadata-action@v5 |
| 165 | + with: |
| 166 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 167 | + tags: | |
| 168 | + type=ref,event=branch |
| 169 | + type=ref,event=pr |
| 170 | + type=semver,pattern={{version}} |
| 171 | + type=semver,pattern={{major}}.{{minor}} |
| 172 | +
|
| 173 | + # Log into the registry. |
| 174 | + # https://github.com/docker/login-action |
| 175 | + - name: Log into registry ${{ env.REGISTRY }} |
| 176 | + uses: docker/login-action@v3 |
| 177 | + with: |
| 178 | + registry: ${{ env.REGISTRY }} |
| 179 | + username: ${{ github.actor }} |
| 180 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 181 | + |
| 182 | + - name: Create manifest list and push |
| 183 | + working-directory: /tmp/digests |
| 184 | + run: | |
| 185 | + docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "${DOCKER_METADATA_OUTPUT_JSON}") \ |
| 186 | + $(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *) |
| 187 | +
|
| 188 | + - name: Inspect image |
| 189 | + run: | |
| 190 | + docker buildx imagetools inspect ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }} |
0 commit comments