on:
  workflow_call:
    inputs:
      isReleaseCandidate:
        required: true
        type: boolean
    secrets:
      githubToken:
        required: true

jobs:
  publish-release:
    name: ${{ inputs.isReleaseCandidate && 'prerelease' || 'release' }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Cache go-build and mod
        uses: actions/cache@v2
        with:
          path: |
            ~/.cache/go-build/
            ~/go/pkg/mod/
          key: go-${{ hashFiles('go.sum') }}
          restore-keys: |
            go-
      - name: Setup Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.17.x
      - name: Setup deps
        run: |
          make install-build-deps
      - name: Publish release
        if: ${{ !inputs.isReleaseCandidate }}
        env:
          GITHUB_TOKEN: ${{ secrets.githubToken }}
        run: |
          ./build/scripts/do-release.sh
      - name: Publish release candidate
        if: ${{ inputs.isReleaseCandidate }}
        env:
          GITHUB_TOKEN: ${{ secrets.githubToken }}
        run: |
          ./build/scripts/do-release-candidate.sh
