|
| 1 | +--- |
| 2 | +name: Image Build and Push |
| 3 | + |
| 4 | +on: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - main |
| 8 | + tags: |
| 9 | + - v* |
| 10 | + pull_request: |
| 11 | + |
| 12 | +defaults: |
| 13 | + run: |
| 14 | + shell: bash |
| 15 | + |
| 16 | +permissions: |
| 17 | + contents: read |
| 18 | + packages: write |
| 19 | + # This is used to complete the identity challenge with sigstore/fulcio. |
| 20 | + id-token: write |
| 21 | + |
| 22 | +env: |
| 23 | + # Use docker.io for Docker Hub if empty |
| 24 | + REGISTRY: ghcr.io |
| 25 | + # github.repository as <account>/<repo> |
| 26 | + IMAGE_NAME: ${{ github.repository }} |
| 27 | + |
| 28 | +jobs: |
| 29 | + build-push: |
| 30 | + strategy: |
| 31 | + fail-fast: false |
| 32 | + matrix: |
| 33 | + platform: |
| 34 | + - linux/amd64 |
| 35 | + |
| 36 | + runs-on: ubuntu-24.04 |
| 37 | + steps: |
| 38 | + - uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1 |
| 39 | + |
| 40 | + - name: Install cosign |
| 41 | + uses: sigstore/cosign-installer@dc72c7d5c4d10cd6bcb8cf6e3fd625a9e5e537da # v3.7.0 |
| 42 | + |
| 43 | + - name: Set up Docker Buildx |
| 44 | + id: buildx |
| 45 | + uses: docker/setup-buildx-action@c47758b77c9736f4b2ef4073d4d51994fabfe349 # v3.7.1 |
| 46 | + with: |
| 47 | + platforms: ${{ matrix.platform }} |
| 48 | + |
| 49 | + - name: Log in to container registry (${{ env.REGISTRY }}) |
| 50 | + uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0 |
| 51 | + with: |
| 52 | + registry: ${{ env.REGISTRY }} |
| 53 | + username: ${{ github.actor }} |
| 54 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 55 | + |
| 56 | + - name: Extract Docker metadata |
| 57 | + id: docker_meta |
| 58 | + uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81 # v5.5.1 |
| 59 | + with: |
| 60 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 61 | + tags: | |
| 62 | + type=edge |
| 63 | + # FIXME: Remove explicit `latest` tag once we start tagging releases |
| 64 | + type=raw,value=latest,enable={{is_default_branch}} |
| 65 | + type=ref,event=tag |
| 66 | + type=sha,format=long |
| 67 | +
|
| 68 | + - name: Build and push Docker image |
| 69 | + id: docker_build_push |
| 70 | + uses: docker/build-push-action@4f58ea79222b3b9dc2c8bbdd6debcef730109a75 # v6.9.0 |
| 71 | + with: |
| 72 | + builder: ${{ steps.buildx.outputs.name }} |
| 73 | + build-args: | |
| 74 | + git_sha=${{ github.sha }} |
| 75 | + cache-from: type=gha,scope=${{ matrix.platform }} |
| 76 | + cache-to: type=gha,mode=max,scope=${{ matrix.platform }} |
| 77 | + labels: ${{ steps.docker_meta.outputs.labels }} |
| 78 | + platforms: ${{ matrix.platform }} |
| 79 | + push: ${{ github.ref == 'refs/heads/main' || startswith(github.event.ref, 'refs/tags/v') }} |
| 80 | + tags: ${{ steps.docker_meta.outputs.tags }} |
| 81 | + |
| 82 | + # Sign the resulting Docker image digest. |
| 83 | + # This will only write to the public Rekor transparency log when the Docker repository is public to avoid leaking |
| 84 | + # data. If you would like to publish transparency data even for private images, pass --force to cosign below. |
| 85 | + # https://github.com/sigstore/cosign |
| 86 | + - name: Sign the published Docker image |
| 87 | + if: ${{ github.ref == 'refs/heads/main' || startswith(github.event.ref, 'refs/tags/v') }} |
| 88 | + # This step uses the identity token to provision an ephemeral certificate against the sigstore community Fulcio |
| 89 | + # instance. |
| 90 | + run: cosign sign --yes ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ steps.docker_build_push.outputs.digest }} |
| 91 | + |
| 92 | + - name: Export digest |
| 93 | + if: ${{ github.ref == 'refs/heads/main' || startswith(github.event.ref, 'refs/tags/v') }} |
| 94 | + run: | |
| 95 | + mkdir -p /tmp/digests |
| 96 | + digest='${{ steps.docker_build_push.outputs.digest }}' |
| 97 | + touch "/tmp/digests/${digest#sha256:}" |
| 98 | +
|
| 99 | + - name: Upload digest |
| 100 | + if: ${{ github.ref == 'refs/heads/main' || startswith(github.event.ref, 'refs/tags/v') }} |
| 101 | + uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3 |
| 102 | + with: |
| 103 | + if-no-files-found: error |
| 104 | + name: digests |
| 105 | + path: /tmp/digests/* |
| 106 | + retention-days: 1 |
| 107 | + |
| 108 | + merge: |
| 109 | + if: ${{ github.ref == 'refs/heads/main' || startswith(github.event.ref, 'refs/tags/v') }} |
| 110 | + needs: |
| 111 | + - build-push |
| 112 | + |
| 113 | + runs-on: ubuntu-24.04 |
| 114 | + steps: |
| 115 | + - name: Download digests |
| 116 | + uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8 |
| 117 | + with: |
| 118 | + name: digests |
| 119 | + path: /tmp/digests |
| 120 | + |
| 121 | + - name: Set up Docker Buildx |
| 122 | + uses: docker/setup-buildx-action@c47758b77c9736f4b2ef4073d4d51994fabfe349 # v3.7.1 |
| 123 | + |
| 124 | + - name: Log in to container registry (${{ env.REGISTRY }}) |
| 125 | + uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0 |
| 126 | + with: |
| 127 | + registry: ${{ env.REGISTRY }} |
| 128 | + username: ${{ github.actor }} |
| 129 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 130 | + |
| 131 | + - name: Extract Docker metadata |
| 132 | + id: docker_meta |
| 133 | + uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81 # v5.5.1 |
| 134 | + with: |
| 135 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 136 | + tags: | |
| 137 | + type=edge |
| 138 | + # FIXME: Remove explicit `latest` tag once we start tagging releases |
| 139 | + type=raw,value=latest,enable={{is_default_branch}} |
| 140 | + type=ref,event=tag |
| 141 | + type=sha,format=long |
| 142 | +
|
| 143 | + - name: Create manifest list and push |
| 144 | + working-directory: /tmp/digests |
| 145 | + run: > |
| 146 | + docker buildx imagetools create \ |
| 147 | + $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "${DOCKER_METADATA_OUTPUT_JSON}") \ |
| 148 | + $(printf ' ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *) |
| 149 | +
|
| 150 | + - name: Inspect image |
| 151 | + run: >- |
| 152 | + docker buildx imagetools inspect \ |
| 153 | + '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.docker_meta.outputs.version }}' |
0 commit comments