name: releaser
on:
  push:
    tags:
      - 'v*'

jobs:
  release-init-kyverno:
    permissions:
      contents: read
      packages: write
      id-token: write
    uses: kyverno/kyverno/.github/workflows/reuse.yaml@main
    with:
      publish_command: docker-publish-initContainer
      digest_command: docker-get-initContainer-digest
      image_name: kyvernopre
      tag: release
      main: cmd/initContainer
    secrets:
      registry_username: ${{ github.actor }}
      registry_password: ${{ secrets.CR_PAT }}

  release-kyverno:
    permissions:
      contents: read
      packages: write
      id-token: write
    uses: kyverno/kyverno/.github/workflows/reuse.yaml@main
    with:
      publish_command: docker-publish-kyverno
      digest_command: docker-get-kyverno-digest
      image_name: kyverno
      tag: release
      main: cmd/kyverno
    secrets:
      registry_username: ${{ github.actor }}
      registry_password: ${{ secrets.CR_PAT }}

  release-kyverno-cli:
    permissions:
      contents: read
      packages: write
      id-token: write
    uses: kyverno/kyverno/.github/workflows/reuse.yaml@main
    with:
      publish_command: docker-publish-cli
      digest_command: docker-get-cli-digest
      image_name: kyverno-cli
      tag: release
      main: cmd/cli/kubectl-kyverno
    secrets:
      registry_username: ${{ github.actor }}
      registry_password: ${{ secrets.CR_PAT }}
  
  create-release:
    runs-on: ubuntu-latest
    needs:
      - release-init-kyverno
      - release-kyverno
      - release-kyverno-cli
    steps:
      - name: Set version
        id: version
        run: echo ::set-output name=version::${GITHUB_REF#refs/*/}

      - name: Checkout
        uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579 # v2.4.0
        with:
          fetch-depth: 0

      - name: Set up Go
        uses: actions/setup-go@424fc82d43fa5a37540bae62709ddcc23d9520d4 # v2.1.5
        with:
          go-version: 1.17

      - name: Cache Go modules
        uses: actions/cache@d9747005de0f7240e5d35a68dca96b3f41b8b340 # v1.2.0
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-
            
      - 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@5df302e5e9e4c66310a6b6493a8865b12c555af2 #v2.8.0
        with:
          version: latest
          args: release --rm-dist --debug --release-notes=release/release-notes.out
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  release-cli-via-krew:
    runs-on: ubuntu-latest
    needs: 
    - create-release
    steps:
      - name: Checkout
        uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579 # v2.4.0

      - name: Unshallow
        run: git fetch --prune --unshallow

      - name: Check Tag
        id: check-tag
        run: |
          if [[ ${{ github.event.ref }} =~ ^refs/tags/v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
              echo ::set-output name=match::true
          fi

      - name: Update new version in krew-index
        if: steps.check-tag.outputs.match == 'true'
        uses: rajatjindal/krew-release-bot@3320c0b546b5d2320613c46762bd3f73e2801bdc # v0.0.38