name: releaser

on:
  push:
    tags:
      - 'v*'

jobs:
  release-images:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
      id-token: write
    steps:
      - name: Checkout
        uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3 # v3.5.0
      - name: Setup build env
        uses: ./.github/actions/setup-build-env
        with:
          build-cache-key: release-images
      - name: Run Trivy vulnerability scanner in repo mode
        uses: aquasecurity/trivy-action@1f0aa582c8c8f5f7639610d6d38baddfea4fdcee # v0.9.2
        with:
          scan-type: 'fs'
          ignore-unfixed: true
          format: 'sarif'
          output: 'trivy-results.sarif'
          severity: 'CRITICAL,HIGH'
      - name: Install Cosign
        uses: sigstore/cosign-installer@9becc617647dfa20ae7b1151972e9b3a2c338a2b # v2.8.1
        with:
          cosign-release: 'v1.13.0'
      - name: Publish kyverno
        uses: ./.github/actions/publish-image
        with:
          makefile-target: ko-publish-kyverno
          registry: ghcr.io
          registry-username: ${{ github.actor }}
          registry-password: ${{ secrets.CR_PAT }}
          repository: ${{ github.repository_owner }}
          sign-image: true
          sbom-name: kyverno
          sbom-repository: ghcr.io/${{ github.repository_owner }}/sbom
          signature-repository: ghcr.io/${{ github.repository_owner }}/signatures
          main-path: ./cmd/kyverno
      - name: Publish kyvernopre
        uses: ./.github/actions/publish-image
        with:
          makefile-target: ko-publish-kyverno-init
          registry: ghcr.io
          registry-username: ${{ github.actor }}
          registry-password: ${{ secrets.CR_PAT }}
          repository: ${{ github.repository_owner }}
          sign-image: true
          sbom-name: kyvernopre
          sbom-repository: ghcr.io/${{ github.repository_owner }}/sbom
          signature-repository: ghcr.io/${{ github.repository_owner }}/signatures
          main-path: ./cmd/kyverno-init
      - name: Publish background-controller
        uses: ./.github/actions/publish-image
        with:
          makefile-target: ko-publish-background-controller
          registry: ghcr.io
          registry-username: ${{ github.actor }}
          registry-password: ${{ secrets.CR_PAT }}
          repository: ${{ github.repository_owner }}
          sign-image: true
          sbom-name: background-controller
          sbom-repository: ghcr.io/${{ github.repository_owner }}/sbom
          signature-repository: ghcr.io/${{ github.repository_owner }}/signatures
          main-path: ./cmd/background-controller
      - name: Publish cleanup-controller
        uses: ./.github/actions/publish-image
        with:
          makefile-target: ko-publish-cleanup-controller
          registry: ghcr.io
          registry-username: ${{ github.actor }}
          registry-password: ${{ secrets.CR_PAT }}
          repository: ${{ github.repository_owner }}
          sign-image: true
          sbom-name: cleanup-controller
          sbom-repository: ghcr.io/${{ github.repository_owner }}/sbom
          signature-repository: ghcr.io/${{ github.repository_owner }}/signatures
          main-path: ./cmd/cleanup-controller
      - name: Publish cli
        uses: ./.github/actions/publish-image
        with:
          makefile-target: ko-publish-cli
          registry: ghcr.io
          registry-username: ${{ github.actor }}
          registry-password: ${{ secrets.CR_PAT }}
          repository: ${{ github.repository_owner }}
          sign-image: true
          sbom-name: cli
          sbom-repository: ghcr.io/${{ github.repository_owner }}/sbom
          signature-repository: ghcr.io/${{ github.repository_owner }}/signatures
          main-path: ./cmd/cli/kubectl-kyverno
      - name: Publish reports-controller
        uses: ./.github/actions/publish-image
        with:
          makefile-target: ko-publish-reports-controller
          registry: ghcr.io
          registry-username: ${{ github.actor }}
          registry-password: ${{ secrets.CR_PAT }}
          repository: ${{ github.repository_owner }}
          sign-image: true
          sbom-name: reports-controller
          sbom-repository: ghcr.io/${{ github.repository_owner }}/sbom
          signature-repository: ghcr.io/${{ github.repository_owner }}/signatures
          main-path: ./cmd/reports-controller

  generate-init-kyverno-provenance:
    needs: release-images
    permissions:
      id-token: write   # To sign the provenance.
      packages: write   # To upload assets to release.
      actions: read     # To read the workflow path.
    # NOTE: The container generator workflow is not officially released as GA.
    uses: slsa-framework/slsa-github-generator/.github/workflows/generator_container_slsa3.yml@v1.5.0
    with:
      image: ghcr.io/${{ github.repository_owner }}/kyvernopre
      digest: "${{ needs.release-init-kyverno.outputs.init-container-digest }}"
      registry-username: ${{ github.actor }}
    secrets:
      registry-password: ${{ secrets.CR_PAT }}

  generate-kyverno-provenance:
    needs: release-images
    permissions:
      id-token: write   # To sign the provenance.
      packages: write   # To upload assets to release.
      actions: read     # To read the workflow path.
    # NOTE: The container generator workflow is not officially released as GA.
    uses: slsa-framework/slsa-github-generator/.github/workflows/generator_container_slsa3.yml@v1.5.0
    with:
      image: ghcr.io/${{ github.repository_owner }}/kyverno
      digest: "${{ needs.release-kyverno.outputs.kyverno-digest }}"
      registry-username: ${{ github.actor }}
    secrets:
      registry-password: ${{ secrets.CR_PAT }}

  generate-background-controller-provenance:
    needs: release-images
    permissions:
      id-token: write   # To sign the provenance.
      packages: write   # To upload assets to release.
      actions: read     # To read the workflow path.
    # NOTE: The container generator workflow is not officially released as GA.
    uses: slsa-framework/slsa-github-generator/.github/workflows/generator_container_slsa3.yml@v1.5.0
    with:
      image: ghcr.io/${{ github.repository_owner }}/background-controller
      digest: "${{ needs.release-background-controller.outputs.background-controller-digest }}"
      registry-username: ${{ github.actor }}
    secrets:
      registry-password: ${{ secrets.CR_PAT }}

  generate-cleanup-controller-provenance:
    needs: release-images
    permissions:
      id-token: write   # To sign the provenance.
      packages: write   # To upload assets to release.
      actions: read     # To read the workflow path.
    # NOTE: The container generator workflow is not officially released as GA.
    uses: slsa-framework/slsa-github-generator/.github/workflows/generator_container_slsa3.yml@v1.5.0
    with:
      image: ghcr.io/${{ github.repository_owner }}/cleanup-controller
      digest: "${{ needs.release-cleanup-controller.outputs.cleanup-controller-digest }}"
      registry-username: ${{ github.actor }}
    secrets:
      registry-password: ${{ secrets.CR_PAT }}

  generate-kyverno-cli-provenance:
    needs: release-images
    permissions:
      id-token: write   # To sign the provenance.
      packages: write   # To upload assets to release.
      actions: read     # To read the workflow path.
    # NOTE: The container generator workflow is not officially released as GA.
    uses: slsa-framework/slsa-github-generator/.github/workflows/generator_container_slsa3.yml@v1.5.0
    with:
      image: ghcr.io/${{ github.repository_owner }}/kyverno-cli
      digest: "${{ needs.release-kyverno-cli.outputs.cli-digest }}"
      registry-username: ${{ github.actor }}
    secrets:
      registry-password: ${{ secrets.CR_PAT }}

  generate-reports-controller-provenance:
    needs: release-images
    permissions:
      id-token: write   # To sign the provenance.
      packages: write   # To upload assets to release.
      actions: read     # To read the workflow path.
    # NOTE: The container generator workflow is not officially released as GA.
    uses: slsa-framework/slsa-github-generator/.github/workflows/generator_container_slsa3.yml@v1.5.0
    with:
      image: ghcr.io/${{ github.repository_owner }}/reports-controller
      digest: "${{ needs.release-reports-controller.outputs.reports-controller-digest }}"
      registry-username: ${{ github.actor }}
    secrets:
      registry-password: ${{ secrets.CR_PAT }}

  create-release:
    runs-on: ubuntu-latest
    needs: release-images
    steps:
      - name: Set version
        id: version
        run: echo "version=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT
      - name: Checkout
        uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3 # v3.5.0
      - name: Setup build env
        uses: ./.github/actions/setup-build-env
      - uses: creekorful/goreportcard-action@1f35ced8cdac2cba28c9a2f2288a16aacfd507f9 # v1.0
      - name: Make Release
        env:
          VERSION: ${{ steps.version.outputs.version }}
        run: |
          rm -rf release
          mkdir release
          make release-notes > release/release-notes.out
          cat release/release-notes.out
      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@f82d6c1c344bcacabba2c841718984797f664a6b # v4.2.0
        with:
          version: latest
          args: release --rm-dist --debug --release-notes=release/release-notes.out
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  push-and-sign-install-manifest:
    runs-on: ubuntu-latest
    needs: create-release
    permissions:
      contents: write # needed to write releases
      id-token: write # needed for keyless signing
      packages: write # needed for ghcr access  
    steps:
      - name: Set version
        id: version
        run: echo "version=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT
      - name: Checkout
        uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3 # v3.5.0
      - name: Setup build env
        uses: ./.github/actions/setup-build-env
      - name: Setup Flux CLI
        uses: fluxcd/flux2/action@dbda8fbdb8b58ed1ee69343025a6091eae0d1828 # v0.41.2
        with:
          version: 0.35.0
      - name: Install Cosign
        uses: sigstore/cosign-installer@9becc617647dfa20ae7b1151972e9b3a2c338a2b # v2.8.1
        with:
          cosign-release: 'v1.13.0'
      - name: Build yaml manifest
        run: make codegen-manifest-release
      - name: Upload install manifest
        uses: svenstaro/upload-release-action@7319e4733ec7a184d739a6f412c40ffc339b69c7 # 2.5.0
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: .manifest/release.yaml
          asset_name: install.yaml
          tag: ${{ github.ref }}
      - name: Upload CRD manifest
        uses: svenstaro/upload-release-action@7319e4733ec7a184d739a6f412c40ffc339b69c7 # 2.5.0
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: config/crds/*.yaml
          file_glob: true
          tag: ${{ github.ref }}
      - name: Login to GHCR
        uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a # v2.1.0
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Push manifests to GHCR with Flux
        env:
          CR_PAT_ARTIFACTS:  ${{ secrets.CR_PAT_ARTIFACTS }}
        run: |
          set -e
          mkdir -p config/.release-manifests
          cp .manifest/release.yaml config/.release-manifests/install.yaml
          cd config/.release-manifests/ && \
          flux push artifact oci://ghcr.io/${{ github.repository_owner }}/manifests/kyverno:${{ steps.version.outputs.version }} \
            --path="." \
            --source="$(git config --get remote.origin.url)" \
            --revision="${{ steps.version.outputs.version }}/$(git rev-parse HEAD)"
      - name: Sign manifests in GHCR with Cosign
        env:
          COSIGN_EXPERIMENTAL: 1
        run: |
          cosign sign ghcr.io/${{ github.repository_owner }}/manifests/kyverno:${{ steps.version.outputs.version }}

  release-cli-via-krew:
    runs-on: ubuntu-latest
    needs: create-release
    steps:
      - name: Checkout
        uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3 # v3.5.0
      - name: Setup build env
        uses: ./.github/actions/setup-build-env
      - name: Check Tag
        id: check-tag
        run: |
          if [[ ${{ github.event.ref }} =~ ^refs/tags/v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
              echo "match=true" >> $GITHUB_OUTPUT
          fi
      - name: Update new version in krew-index
        if: steps.check-tag.outputs.match == 'true'
        uses: rajatjindal/krew-release-bot@df3eb197549e3568be8b4767eec31c5e8e8e6ad8 # v0.0.46
