diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 23c1ba2e..d09f05ed 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -29,17 +29,22 @@ jobs: runs-on: 'ubuntu-latest' permissions: contents: read + # kics-scan ignore-line + id-token: write # needed for signing the images with GitHub OIDC Token outputs: skip_tests: ${{ steps.check_event.outputs.event_type == 'release' || (steps.check_event.outputs.event_type == 'workflow_dispatch' && github.event.inputs.skip_tests == 'true') }} - image: ${{ env.IMAGE_REGISTRY }}/${{ vars.QUAY_ORG }}/${{ env.IMAGE_NAME }}@${{ steps.build-image.outputs.digest }} + image: ${{ steps.set_image_repo.outputs.image_repo }}@${{ steps.build-and-push.outputs.digest }} steps: - name: Set up QEMU uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 + + - name: Set up cosign + uses: sigstore/cosign-installer@v3.1.1 - name: Login to Quay uses: docker/login-action@v3 @@ -47,6 +52,11 @@ jobs: username: ${{ secrets.QUAY_USER }} password: ${{ secrets.QUAY_TOKEN }} registry: ${{ env.IMAGE_REGISTRY }} + + - name: Set image repository + id: set_image_repo + run: | + echo "image_repo=${{ env.IMAGE_REGISTRY }}/${{ vars.QUAY_ORG }}/${{ env.IMAGE_NAME }}" >> "$GITHUB_OUTPUT" - name: Check if triggered by release or workflow dispatch id: check_event @@ -72,17 +82,18 @@ jobs: - name: Build and export to Docker uses: docker/build-push-action@v5 + id: build-and-export with: load: true no-cache: ${{ env.NO_CACHE == 'true' }} cache-from: type=gha cache-to: type=gha,mode=max - tags: ${{ env.IMAGE_REGISTRY }}/${{ vars.QUAY_ORG }}/${{ env.IMAGE_NAME }}:${{ env.TAG }} + tags: ${{ steps.set_image_repo.outputs.image_repo }}:${{ env.TAG }} - name: Pre-push Image Scan uses: aquasecurity/trivy-action@0.16.0 with: - image-ref: ${{ env.IMAGE_REGISTRY }}/${{ vars.QUAY_ORG }}/${{ env.IMAGE_NAME }}:${{ env.TAG }} + image-ref: ${{ steps.set_image_repo.outputs.image_repo }}:${{ env.TAG }} exit-code: 1 skip-files: "**/.venv/lib/**/METADATA" scanners: secret @@ -91,10 +102,25 @@ jobs: # Does not rebuild. Uses internal cache from previous step. - name: Build and Push uses: docker/build-push-action@v5 - id: build-image + id: build-and-push with: push: true - tags: ${{ env.IMAGE_REGISTRY }}/${{ vars.QUAY_ORG }}/${{ env.IMAGE_NAME }}:${{ env.TAG }} + tags: ${{ steps.set_image_repo.outputs.image_repo }}:${{ env.TAG }} + + - name: Sign the image with GitHub OIDC Token + run: cosign sign --yes "$IMAGE@$DIGEST" + env: + DIGEST: ${{ steps.build-and-push.outputs.digest }} + IMAGE: ${{ steps.set_image_repo.outputs.image_repo }} + + - name: Verify image + run: | + cosign verify "$IMAGE@$DIGEST" --certificate-identity-regexp="$SUBJECT" \ + --certificate-oidc-issuer=https://token.actions.githubusercontent.com + env: + SUBJECT: https://github\.com/${{ github.repository_owner }}/trestle-bot/\.github/.+ + IMAGE: ${{ steps.set_image_repo.outputs.image_repo }} + DIGEST: ${{ steps.build-and-push.outputs.digest }} test: permissions: @@ -103,4 +129,4 @@ jobs: if: ${{ needs.publish-image.outputs.skip_tests != 'true' }} uses: ./.github/workflows/e2e.yml with: - image: ${{ needs.publish-image.outputs.image }} \ No newline at end of file + image: ${{ needs.publish-image.outputs.image }}