|
| 1 | +# Based on |
| 2 | +# - https://docs.github.com/en/actions/use-cases-and-examples/publishing-packages/publishing-docker-images |
| 3 | +# - https://docs.docker.com/build/ci/github-actions/multi-platform/#distribute-build-across-multiple-runners |
| 4 | +# and the branchwater config |
| 5 | +# - https://github.com/sourmash-bio/branchwater/blob/main/.github/workflows/docker-images.yml |
| 6 | + |
| 7 | +name: Create and publish Docker images |
| 8 | + |
| 9 | +on: |
| 10 | + push: |
| 11 | + branches: |
| 12 | + - 'main' |
| 13 | + tags: |
| 14 | + - 'v*' |
| 15 | + pull_request: |
| 16 | + paths: |
| 17 | + - .github/workflows/docker-images.yml |
| 18 | + - Dockerfile |
| 19 | +## disabling for most PRs, too many builds |
| 20 | +# branches: |
| 21 | +# - 'main' |
| 22 | + |
| 23 | +env: |
| 24 | + REGISTRY: ghcr.io |
| 25 | + IMAGE_NAME: ${{ github.repository }} |
| 26 | + |
| 27 | +jobs: |
| 28 | + build: |
| 29 | + runs-on: ${{ matrix.os }} |
| 30 | + strategy: |
| 31 | + matrix: |
| 32 | + target: [web, worker] |
| 33 | + platform: |
| 34 | + - linux/amd64 |
| 35 | + - linux/arm64 |
| 36 | + include: |
| 37 | + - platform: linux/amd64 |
| 38 | + os: ubuntu-latest |
| 39 | + - platform: linux/arm64 |
| 40 | + os: ubuntu-24.04-arm |
| 41 | + |
| 42 | + permissions: |
| 43 | + contents: read |
| 44 | + packages: write |
| 45 | + id-token: write |
| 46 | + |
| 47 | + steps: |
| 48 | + - name: Delete huge unnecessary tools folder |
| 49 | + run: | |
| 50 | + rm -rf /opt/hostedtoolcache |
| 51 | + cd /opt |
| 52 | + find . -maxdepth 1 -mindepth 1 '!' -path ./containerd '!' -path ./actionarchivecache '!' -path ./runner '!' -path ./runner-cache -exec rm -rf '{}' ';' |
| 53 | +
|
| 54 | + - name: Checkout repository |
| 55 | + uses: actions/checkout@85e6279cec87321a52edac9c87bce653a07cf6c2 |
| 56 | + |
| 57 | + - name: Log in to the Container registry |
| 58 | + uses: docker/login-action@327cd5a69de6c009b9ce71bce8395f28e651bf99 |
| 59 | + with: |
| 60 | + registry: ${{ env.REGISTRY }} |
| 61 | + username: ${{ github.actor }} |
| 62 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 63 | + |
| 64 | + - name: Set up Docker Buildx |
| 65 | + uses: docker/setup-buildx-action@6524bf65af31da8d45b59e8c27de4bd072b392f5 |
| 66 | + |
| 67 | + - name: Prepare |
| 68 | + run: | |
| 69 | + platform=${{ matrix.platform }} |
| 70 | + echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV |
| 71 | +
|
| 72 | + - name: Extract metadata (tags, labels) for Docker |
| 73 | + id: meta |
| 74 | + uses: docker/metadata-action@8e1d5461f02b7886d3c1a774bfbd873650445aa2 |
| 75 | + with: |
| 76 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 77 | + |
| 78 | + - name: Build and push by digest |
| 79 | + id: build |
| 80 | + uses: docker/build-push-action@ca877d9245402d1537745e0e356eab47c3520991 |
| 81 | + with: |
| 82 | + context: . |
| 83 | + platforms: ${{ matrix.platform }} |
| 84 | + labels: ${{ steps.meta.outputs.labels }} |
| 85 | + target: ${{ matrix.target }} |
| 86 | + outputs: type=image,"name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}",push-by-digest=true,name-canonical=true,push=true |
| 87 | + cache-from: type=gha |
| 88 | + cache-to: type=gha,mode=max |
| 89 | + # don't push on PRs |
| 90 | + push: ${{ ! startsWith(github.ref, 'refs/pull/') }} |
| 91 | + |
| 92 | + - name: Export digest |
| 93 | + run: | |
| 94 | + mkdir -p ${{ runner.temp }}/digests |
| 95 | + digest="${{ steps.build.outputs.digest }}" |
| 96 | + touch "${{ runner.temp }}/digests/${digest#sha256:}" |
| 97 | +
|
| 98 | + - name: Upload digest |
| 99 | + uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 |
| 100 | + with: |
| 101 | + name: digests-${{ matrix.target }}-${{ env.PLATFORM_PAIR }} |
| 102 | + path: ${{ runner.temp }}/digests/* |
| 103 | + if-no-files-found: error |
| 104 | + retention-days: 1 |
| 105 | + |
| 106 | + merge: |
| 107 | + runs-on: ubuntu-latest |
| 108 | + # avoid tagging on PRs |
| 109 | + if: ${{ ! startsWith(github.ref, 'refs/pull/') }} |
| 110 | + needs: |
| 111 | + - build |
| 112 | + |
| 113 | + strategy: |
| 114 | + matrix: |
| 115 | + target: [web, worker] |
| 116 | + |
| 117 | + permissions: |
| 118 | + contents: read |
| 119 | + packages: write |
| 120 | + attestations: write |
| 121 | + id-token: write |
| 122 | + |
| 123 | + steps: |
| 124 | + - name: Download digests |
| 125 | + uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 |
| 126 | + with: |
| 127 | + path: ${{ runner.temp }}/digests |
| 128 | + pattern: digests-${{ matrix.target }}-* |
| 129 | + merge-multiple: true |
| 130 | + |
| 131 | + - name: Log in to the Container registry |
| 132 | + uses: docker/login-action@327cd5a69de6c009b9ce71bce8395f28e651bf99 |
| 133 | + with: |
| 134 | + registry: ${{ env.REGISTRY }} |
| 135 | + username: ${{ github.actor }} |
| 136 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 137 | + |
| 138 | + - name: Set up Docker Buildx |
| 139 | + uses: docker/setup-buildx-action@6524bf65af31da8d45b59e8c27de4bd072b392f5 |
| 140 | + |
| 141 | + - name: Extract metadata (tags, labels) for Docker |
| 142 | + id: meta |
| 143 | + uses: docker/metadata-action@8e1d5461f02b7886d3c1a774bfbd873650445aa2 |
| 144 | + with: |
| 145 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 146 | + tags: | |
| 147 | + type=ref,event=branch,prefix=${{ matrix.target }}- |
| 148 | + type=ref,event=pr,prefix=${{ matrix.target }}-pr- |
| 149 | + type=semver,prefix=${{ matrix.target }}-,pattern={{version}} |
| 150 | +
|
| 151 | + - name: Create manifest list and push |
| 152 | + working-directory: ${{ runner.temp }}/digests |
| 153 | + run: | |
| 154 | + docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ |
| 155 | + $(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *) |
| 156 | +
|
| 157 | + - name: Inspect image |
| 158 | + run: | |
| 159 | + docker buildx imagetools inspect ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }} |
| 160 | +
|
| 161 | +# - name: Generate artifact attestation |
| 162 | +# uses: actions/attest-build-provenance@v2 |
| 163 | +# with: |
| 164 | +# subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 165 | +# subject-digest: ${{ steps.build.outputs.digest }} |
| 166 | +# push-to-registry: true |
0 commit comments