on:
  workflow_call:
    inputs:
      isReleaseCandidate:
        required: true
        type: boolean
      name:
        required: true
        type: string
    secrets:
      githubToken:
        required: true
      slackToken:
        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.18.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
      - name: get version
        id: get_version
        run: echo ::set-output name=VERSION::$(echo ${GITHUB_REF#refs/*/})
      - name: slack on success - team-pitch-black
        if: ${{ success() }}
        uses: actions-ecosystem/action-slack-notifier@fc778468d09c43a6f4d1b8cccaca59766656996a
        with:
          slack_token: ${{ secrets.slackToken }}
          message: ":cool-doge: ${{ inputs.name }} successful for tag <https://github.com/weaveworks/eksctl/releases/tag/${{ steps.get_version.outputs.VERSION }}|${{ steps.get_version.outputs.VERSION }}>"
          channel: team-pitch-black
          color: green
          verbose: false
      - name: slack on failure - team-pitch-black
        if: ${{ failure() }}
        uses: actions-ecosystem/action-slack-notifier@fc778468d09c43a6f4d1b8cccaca59766656996a
        with:
          slack_token: ${{ secrets.slackToken }}
          message: ":ahhhhhhhhh: ${{ inputs.name }} has failed for tag <https://github.com/weaveworks/eksctl/releases/tag/${{ steps.get_version.outputs.VERSION }}|${{ steps.get_version.outputs.VERSION }}>"
          channel: team-pitch-black
          color: red
          verbose: true
      - name: slack on success - aws-dev
        if: ${{ success() }}
        uses: actions-ecosystem/action-slack-notifier@fc778468d09c43a6f4d1b8cccaca59766656996a
        with:
          slack_token: ${{ secrets.slackToken }}
          message: ":tada: <https://github.com/weaveworks/eksctl/releases/tag/${{ steps.get_version.outputs.VERSION }}|${{ steps.get_version.outputs.VERSION }}> released!"
          channel: aws-dev
          color: green
          verbose: false
