name: Release

on:
  push:
    tags:
      - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10

jobs:
  release:
    name: release
    runs-on: ubuntu-latest
    steps:
      - name: Check out code into the Go module directory
        uses: actions/checkout@v3

      # Ugly hack to get the tag name
      # github.ref gives the full reference like refs.tags.v0.0.1-beta1
      - name: Branch name
        id: branch_name
        run: |
          echo ::set-output name=TAG_NAME::${GITHUB_REF#refs/tags/}

      - name: Set up Go
        uses: actions/setup-go@v4
        with:
          go-version-file: go.mod
          check-latest: true

      - name: Build binaries
        id: build_bins
        env:
          SEGMENT_WRITE_KEY: ${{ secrets.SEGMENT_WRITE_KEY }}
          TAG_NAME: ${{ steps.branch_name.outputs.TAG_NAME }}
        run: make build-all

      - name: Create release and upload binaries
        uses: softprops/action-gh-release@v1
        if: startsWith(github.ref, 'refs/tags/')
        with:
          files: |
            bin/k0sctl-*
            bin/checksums.txt
          body_path: bin/checksums.md
          tag_name: ${{ steps.branch_name.outputs.TAG_NAME }}
          name: ${{ steps.branch_name.outputs.TAG_NAME }}
          draft: true # So we can manually edit before publishing
          prerelease: ${{ contains(steps.branch_name.outputs.TAG_NAME, '-') }} # v0.1.2-beta1, 1.2.3-rc1
