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@v3
      - name: Cache go-build and mod
        uses: actions/cache@v3
        with:
          path: |
            ~/.cache/go-build/
            ~/go/pkg/mod/
          key: go-${{ hashFiles('go.sum') }}
          restore-keys: |
            go-
      - name: Setup Go
        uses: actions/setup-go@4d34df0c2316fe8122ab82dc22947d607c0c91f9 #v4.0.0
        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 "version=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT
      - 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
