name: Deploy

on:
  push:
    branches:
      - master
      - main

  issue_comment:
    types: [ created ]

concurrency: master-main

env:
  GITHUB_TOKEN: ${{ secrets.TOPTAL_DEVBOT_TOKEN }}
  GCR_ACCOUNT_KEY: ${{ secrets.GCR_ACCOUNT_KEY }}
  REPOSITORY_NAME: ${{ github.event.repository.name }}
  IMAGE_NAME: ${{ github.event.repository.name }}
  SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_SOURCEMAP_UPLOAD_TOKEN }}

jobs:
  check-event:
    name: Check Event
    timeout-minutes: 5
    runs-on: ubuntu-latest
    if: >
      (
        github.event_name == 'push'
      ) || (
        github.event_name == 'issue_comment' &&
        github.event.comment.body == '@toptal-bot run deploy-staging'
      )
    outputs:
      sha: ${{ steps.specify-sha.outputs.result }}
      should-deploy-to-staging: ${{ steps.deployment-necessity.outputs.should-deploy-to-staging }}
      should-deploy-to-production: ${{ steps.deployment-necessity.outputs.should-deploy-to-production }}
    steps:
      - id: specify-sha
        uses: toptal/davinci-github-actions/get-workflow-sha@v20.0.0

      - name: Checkout
        uses: actions/checkout@v3
        with:
          token: ${{ env.GITHUB_TOKEN }}
          ref: ${{ steps.specify-sha.outputs.result }}

      - name: Read davinci configuration
        id: davinci-config-data
        if: hashFiles('davinci.yaml') != ''
        uses: KJ002/read-yaml@1.6
        with:
          file: './davinci.yaml'
          key-path: '["master"]'

      - name: Check if it should be deployed
        id: deployment-necessity
        uses: actions/github-script@v3.0.0
        env:
          DAVINCI_CONFIG_DATA: ${{ steps.davinci-config-data.outputs.data }}
          EVENT_NAME: ${{ github.event_name }}
        with:
          github-token: ${{ env.GITHUB_TOKEN }}
          result-encoding: string
          script: |
            const {DAVINCI_CONFIG_DATA, EVENT_NAME} = process.env
            const configData = DAVINCI_CONFIG_DATA ? JSON.parse(DAVINCI_CONFIG_DATA) : null

            if (EVENT_NAME === 'issue_comment') {
              core.setOutput('should-deploy-to-staging', true)
              core.setOutput('should-deploy-to-production', false)
            } else {
              core.setOutput('should-deploy-to-staging', configData ? configData.deploy_staging : false)
              core.setOutput('should-deploy-to-production', configData ? configData.deploy : false)
            }

  build-push-release-image:
    name: Build & Push release image
    timeout-minutes: 45
    runs-on: ubuntu-latest
    needs: [ check-event ]
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          token: ${{ env.GITHUB_TOKEN }}
          ref: ${{ needs.check-event.outputs.sha }}

      - uses: toptal/davinci-github-actions/build-push-release-image@v20.0.0
        with:
          sha: ${{ needs.check-event.outputs.sha }}
          repository-name: ${{ github.event.repository.name }}
          environment: ${{ env.ENVIRONMENT }}

      - uses: toptal/davinci-github-actions/upload-source-maps@v20.0.0
        continue-on-error: true
        with:
          sentry-project: ${{ github.event.repository.name }}

  deploy-to-staging:
    uses: ./.github/workflows/davinci-deploy-staging.yml
    name: Deploy to Staging
    needs: [ build-push-release-image, check-event ]
    if: ${{ fromJSON(needs.check-event.outputs.should-deploy-to-staging) == true }}
    secrets:
      TOPTAL_DEVBOT_TOKEN: ${{ secrets.TOPTAL_DEVBOT_TOKEN }}
      TOPTAL_BOT_JENKINS_API_TOKEN: ${{ secrets.TOPTAL_BOT_JENKINS_API_TOKEN }}
    with:
      sha: ${{ needs.check-event.outputs.sha }}

  deploy-to-production:
    uses: ./.github/workflows/davinci-deploy-production.yml
    name: Deploy to Production
    needs: [ build-push-release-image, check-event ]
    if: ${{ fromJSON(needs.check-event.outputs.should-deploy-to-production) == true }}
    secrets:
      TOPTAL_DEVBOT_TOKEN: ${{ secrets.TOPTAL_DEVBOT_TOKEN }}
      TOPTAL_BOT_JENKINS_DEPLOYMENT_TOKEN: ${{ secrets.TOPTAL_BOT_JENKINS_DEPLOYMENT_TOKEN }}
    with:
      sha: ${{ needs.check-event.outputs.sha }}
