name: Prepare Release

# Only release when a new GH release branch is pushed
on:
  push:
    branches:
      - 'release/v[0-9]+.[0-9]+.[0-9]+'

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      # Checkout and set env
      - name: Checkout
        uses: actions/checkout@v3.1.0
      - id: load-version
        run: |
          source $GITHUB_WORKSPACE/versions.env
          echo "GO_VERSION=$GO_VERSION" >> $GITHUB_ENV
      - name: Configure Git
        run: |
          git config user.name "$GITHUB_ACTOR"
          git config user.email "$GITHUB_ACTOR@users.noreply.github.com"
      - name: Set up Go
        uses: actions/setup-go@v3.3.1
        with:
          go-version: ${{ env.GO_VERSION }}
      - name: Setup kubecfg
        run: |
          mkdir -p ~/bin
          curl -sLf https://github.com/kubecfg/kubecfg/releases/download/v0.26.0/kubecfg_Linux_X64 >~/bin/kubecfg
          chmod +x ~/bin/kubecfg

      - name: Install dependencies
        run: |
          go install gotest.tools/gotestsum@v1.8.1

      # Run tests
      - name: Tests
        run: make test

      # Tag for GoReleaser from release branch name
      - name: Tag Release
        run: |
          RELEASE_BRANCH="${{ github.ref }}"
          VERSION_TAG=$(echo "${RELEASE_BRANCH}" | awk -F'/' '{print $NF}')
          git tag -a "${VERSION_TAG}" -m "Tag autogenerated ${VERSION_TAG}"
          git push origin "${VERSION_TAG}"
