name: Release Workflow
on:
  workflow_dispatch:
    inputs:
      release_candidate_version:
        description: "Release Candidate Version:"
        required: true

jobs:
  go_build_and_test:
    uses: ./.github/workflows/reusable_go_build.yml
    with:
      goBuildTag: main

  define_version_job:
    runs-on: ubuntu-latest
    needs: [go_build_and_test]
    steps:
      - uses: actions/checkout@v3
      - id: define_version
        name: Define release version
        run: |-
          OUTPUT_VERSION=$(bash scripts/define_release_version.sh ${{ github.event.inputs.release_candidate_version }})
          echo "detected version = $OUTPUT_VERSION"
          echo ::set-output name=version::$(echo $OUTPUT_VERSION)
    outputs:
      version: ${{ steps.define_version.outputs.version }}

  release:
    env:
      GITHUB_ACTION_RUN_ID: ${{ github.run_id }}
      GORELEASER_VERSION: 0.155.1
      GITHUB_TOKEN: ${{ secrets.GIT_TOKEN }}
      CLOUDFRONT_DISTRIBUTION_ID: ${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }}
    runs-on: ubuntu-latest
    needs: [ go_build_and_test, define_version_job ]
    steps:
      - uses: actions/checkout@v3
      - name: Extract branch name
        shell: bash
        run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
        id: extract_branch
      - run: |
          echo "Triggered by: ${{ github.actor }}!"
          branch_name=${{ steps.extract_branch.outputs.branch }}
          commit_sha=${{ github.sha }}

          echo "branch name: $branch_name"
          if [ "$branch_name" != "main" ]; then
              echo "Release should build only from main branch"
              exit 1
          fi
      - name: Setup Go environment
        uses: actions/setup-go@v3
        with:
          go-version: 1.19
      - name: GitHub Config Identity
        run: |
          git config --global user.email "ci@datree.io"
          git config --global user.name "datree-ci"
      - name: Set AWS credentials
        uses: aws-actions/configure-aws-credentials@v1-node16
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-1
      - name: Run deploy prod release file
        env:
          RELEASE_VERSION: ${{ needs.define_version_job.outputs.version }}
        run: bash ./scripts/release.sh
        shell: bash

  brew_push_formula:
    name: Brew push formula
    runs-on: ubuntu-latest
    needs: [ release, define_version_job ]
    env:
      RELEASE_VERSION: ${{ needs.define_version_job.outputs.version }}
      GITHUB_TOKEN: ${{ secrets.GIT_TOKEN }}
    steps:
      - uses: actions/checkout@v3
      - name: GitHub Config Identity
        run: |
          git config --global user.email "ci@datree.io"
          git config --global user.name "datree-ci"
      - run: bash ./scripts/brew_push_formula.sh production ${{ env.RELEASE_VERSION }}
        shell: bash

  release-docker:
    env:
      REPO_NAME: datree
      IMAGE_NAME: datree
      CI_IMAGE_NAME: datreeci
    name: Release container image to public registries
    needs: [ release, define_version_job ]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: docker/setup-buildx-action@v2
      - name: Log in to DockerHub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Build and push datree image to registries
        uses: docker/build-push-action@v3
        with:
          context: .
          file: dockerfiles/image-datree/Dockerfile
          platforms: linux/amd64
          push: true
          tags: |
            ${{ env.REPO_NAME }}/${{ env.IMAGE_NAME }}:latest
            ${{ env.REPO_NAME }}/${{ env.IMAGE_NAME }}:${{ needs.define_version_job.outputs.version }}
          cache-from: type=gha
          cache-to: type=gha
      - name: Build and push datree CI image to registries
        uses: docker/build-push-action@v3
        with:
          context: .
          file: dockerfiles/image-datreeci/Dockerfile
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ env.REPO_NAME }}/${{ env.CI_IMAGE_NAME }}:latest
          cache-from: type=gha
          cache-to: type=gha
