name: Kubernetes-sigs/krew CI
on:
  push:
    branches:
      - '*'
    tags:
      - 'v*.*.*'
  pull_request:
    branches:
      - '*'

jobs:

  build:
    runs-on: ubuntu-latest
    steps:

    - name: Set up Go ${{ matrix.goVer }}
      uses: actions/setup-go@v5
      with:
        go-version: '1.22'
        check-latest: true
        cache: true
      id: go

    - name: Check out code into the Go module directory
      uses: actions/checkout@v4
      with:
        fetch-depth: 0

    - name: Install dependencies
      run: go mod download

    - name: Install gox
      run: hack/install-gox.sh

    - name: Ensure go.mod is already tidied
      run: go mod tidy && git diff --no-patch --exit-code

    - name: Verify code patterns
      run: hack/verify-code-patterns.sh

    - name: Verify boilerplate
      run: hack/verify-boilerplate.sh

    - name: Run code lint
      run: hack/run-lint.sh

    - name: Run unit tests
      run: go test -short ./...

    - name: Make binaries && verify krew installation
      run: hack/make-all.sh

    - name: Ensure kubectl installed
      run: hack/ensure-kubectl-installed.sh

    - name: Verify installation
      run: hack/verify-installation.sh

    - name: Run integration tests
      run: hack/run-integration-tests.sh

    - name: Verify index migration from 0.3.x to 0.4.x
      run: hack/verify-index-migration.sh

    - name: Make release notes
      if: contains(github.ref, 'tags')
      run: |
        echo 'RELEASE_NOTES<<EOF' >> $GITHUB_ENV
        TAG=$GITHUB_REF_NAME hack/make-release-notes.sh >> $GITHUB_ENV
        echo 'EOF' >> $GITHUB_ENV

    - name: Create a new release
      if: contains(github.ref, 'tags')
      id: create_release
      uses: softprops/action-gh-release@v2
      with:
        name: Release ${{ github.ref_name }}
        files: out/krew*
        body: ${{ env.RELEASE_NOTES }}
        fail_on_unmatched_files: true
