name: Deploy Temploy

on:
  pull_request:
    branches: [master]
  issue_comment:
    types: [ created ]

concurrency:
  group: temploy-${{ github.head_ref }}
  cancel-in-progress: true

env:
  GITHUB_TOKEN: ${{ secrets.TOPTAL_DEVBOT_TOKEN }}

jobs:
  build-push-image:
    name: Build & Push image
    if: >
      github.event.pull_request || (
        github.event.issue.pull_request &&
        github.event.comment.body == '@toptal-bot run temploy'
      )
    timeout-minutes: 45
    env:
      GCR_ACCOUNT_KEY: ${{ secrets.GCR_ACCOUNT_KEY }}
    runs-on: ubuntu-latest
    outputs:
      branch: ${{ steps.get-branch.outputs.branch }}
      sha: ${{ steps.get-branch.outputs.sha }}
      repository_name: ${{ steps.get-repo.outputs.repository_name }}
    steps:
      - name: Cancel Previous Runs
        uses: styfle/cancel-workflow-action@d1af9509c9794689332b13f11ada1dfd07fc06ce

      - id: branch
        uses: xt0rted/pull-request-comment-branch@v2.0.0

      - name: Specify branch
        id: get-branch
        run: |
          echo "branch=${{ steps.branch.outputs.head_ref }}" >> $GITHUB_OUTPUT
          echo "sha=${{ steps.branch.outputs.head_sha }}" >> $GITHUB_OUTPUT

      - name: Checkout
        uses: actions/checkout@v3
        with:
          ref: ${{ steps.get-branch.outputs.branch }}

      - name: Specify repository name
        id: get-repo
        run: echo repository_name=$(echo "$GITHUB_REPOSITORY" | awk -F / '{print $2}' | sed -e "s/:refs//") >> $GITHUB_OUTPUT

      - name: Checkout davinci
        uses: actions/checkout@v3
        with:
          repository: toptal/davinci
          token: ${{ env.GITHUB_TOKEN }}
          path: davinci

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: 20

      - uses: toptal/davinci-github-actions/pnpm-install@v20.0.0

      - name: Build
        run: pnpm build
        env:
          GITHUB_TOKEN: ${{ env.GITHUB_TOKEN }}

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v3
        with:
          images: gcr.io/toptal-hub/${{ steps.get-repo.outputs.repository_name }}-release
          tags: |
            type=raw,value={{branch}}
            type=raw,value={{sha}}
            type=raw,value=${{ steps.get-branch.outputs.sha }}
          flavor: latest=false

      - name: Login to GCloud Registry
        uses: docker/login-action@v2
        with:
          registry: gcr.io
          username: _json_key
          password: ${{ env.GCR_ACCOUNT_KEY }}

      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v2

      - name: Build and push image
        uses: docker/build-push-action@v2
        with:
          tags: ${{ steps.meta.outputs.tags }}
          push: true
          context: .
          file: ./davinci/packages/ci/src/configs/docker/Dockerfile.gha-deploy
          build-args: |
            ENV_RUNTIME_ENTRYPOINT=./davinci/packages/ci/src/configs/docker/env-runtime.entrypoint.sh
            DIST_FOLDER=./dist
            NGINX_CONFIG=./davinci/packages/davinci/ci/src/configs/docker/nginx-vhost.conf
            VERSION=${{ steps.get-branch.outputs.sha }}

  deploy-temploy:
    name: Deploy Temploy
    timeout-minutes: 45
    runs-on: ubuntu-latest
    needs: [build-push-image]
    permissions:
      contents: read
      id-token: write
    env:
      REPOSITORY_NAME: ${{ needs.build-push-image.outputs.repository_name }}
      RELEASE: ${{ needs.build-push-image.outputs.repository_name }}-pr-${{ github.event.number || github.event.issue.number }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          ref: ${{ needs.build-push-image.outputs.branch }}

      - name: GSM Secrets
        id: secrets_manager
        uses: toptal/actions/gsm-secrets@main
        with:
          workload_identity_provider: projects/858873486241/locations/global/workloadIdentityPools/gha-pool/providers/github-com
          service_account: gha-davinci@toptal-ci.iam.gserviceaccount.com
          secrets_name: |-
            JENKINS_CLIENT_ID:toptal-ci/JENKINS_CLIENT_ID
            JENKINS_URL:toptal-ci/JENKINS_URL
            JENKINS_SA_CREDENTIALS:toptal-ci/JENKINS_SA_CREDENTIALS
            TOPTAL_TRIGGERBOT_TOKEN:toptal-ci/TOPTAL_TRIGGERBOT_TOKEN
            TOPTAL_TRIGGERBOT_USERNAME:toptal-ci/TOPTAL_TRIGGERBOT_USERNAME

      - name: Parse secrets
        id: parse_secrets
        uses: toptal/actions/expose-json-outputs@main
        with:
          json: ${{ steps.secrets_manager.outputs.secrets }}

      - uses: toptal/davinci-github-actions/extract-env-variables@v20.0.0
        id: env-variables
        with:
          github-token: ${{ env.GITHUB_TOKEN }}
          filename: .env.temploy

      - name: Trigger temploy job
        uses: toptal/actions/trigger-jenkins-job@main
        with:
          jenkins_url: ${{ steps.parse_secrets.outputs.JENKINS_URL }}
          jenkins_user: ${{ steps.parse_secrets.outputs.TOPTAL_TRIGGERBOT_USERNAME }}
          jenkins_token: ${{ steps.parse_secrets.outputs.TOPTAL_TRIGGERBOT_TOKEN }}
          jenkins_client_id: ${{ steps.parse_secrets.outputs.JENKINS_CLIENT_ID }}
          jenkins_sa_credentials: ${{ steps.parse_secrets.outputs.JENKINS_SA_CREDENTIALS }}
          job_name: '${{ env.REPOSITORY_NAME }}/job/${{ env.REPOSITORY_NAME }}-temploy-helm-run'
          job_params: |
            {
              "REPOSITORY_NAME": "${{ env.REPOSITORY_NAME }}",
              "TAG": "${{ needs.build-push-image.outputs.sha }}",
              "ENV": "${{ steps.env-variables.outputs.variables }}",
              "RELEASE": "${{ env.RELEASE }}"
            }
          job_timeout: '1200'

      - name: Post temploy link
        uses: actions/github-script@v3.0.0
        with:
          github-token: ${{ env.GITHUB_TOKEN }}
          script: |
            const { issue: { number: issue_number }, repo: { owner, repo }  } = context
            const body = 'Temploy was scheduled, it will become available in a few minutes and available at https://t-${{ env.RELEASE }}.toptal.net'
            github.issues.createComment({ issue_number, owner, repo, body })
