name: Manual Deployment Trigger
on:
  workflow_dispatch:
    inputs:
      environment:
        description: 'The environment to deploy to'
        required: true
        default: 'dev'
      ref:
        description: 'Branch or commit SHA to deploy from'
        required: true
        default: 'master'
      task:
        description: 'The task to perform. (`deploy` for global or specify regional tasks)'
        required: false
        default: 'deploy' # deploy, euc1, euc1-dark, use1, use1-dark, apn1, apn1-dark
jobs:
  deploy:
    runs-on: ubuntu-latest
    if: contains( 'dev,test,test-ci,stage,prod', github.event.inputs.environment )
    timeout-minutes: 5
    steps:
      - uses: actions/checkout@v3.3.0
        name: validate ref existence
        with:
          fetch-depth: 0
          ref: ${{ github.event.inputs.ref }}
      - name: get canonical commit ref
        id: canonical-commit-ref
        run: echo "ref=$(git log --grep 'skip ci' --invert-grep -n 1 --pretty='format:%H')" >> $GITHUB_OUTPUT
      - name: check commit ref presence in default branch
        if: contains( 'stage,prod', github.event.inputs.environment )
        run: git branch --remote --contains ${{ steps.canonical-commit-ref.outputs.ref }} | grep -m 1 -q "${{ github.event.repository.default_branch }}" && echo "commit ref found in default branch ✅" || (echo "::error::Only commit refs present in default branch can be deployed to this env\!" && false)
      - name: trigger deployment event
        run: |
          curl -X POST https://api.github.com/repos/${{ github.repository }}/deployments \
          -u ${{ secrets.TEAM_GITHUB_TOKEN }} \
          --data "{\"ref\": \"${{ steps.canonical-commit-ref.outputs.ref }}\", \"auto_merge\": false, \"environment\": \"${{ github.event.inputs.environment }}\", \"task\": \"${{ github.event.inputs.task }}\", \"description\": \"Deploy ref ${{ github.event.inputs.ref }} to ${{ github.event.inputs.environment }}\", \"required_contexts\": [], \"production_environment\": ${{ github.event.inputs.environment == 'prod' }} }"
  no-deploy:
    runs-on: ubuntu-latest
    if: ${{ !contains( 'dev,test,test-ci,stage,prod', github.event.inputs.environment ) }}
    timeout-minutes: 5
    steps:
      - name: log invalid input
        run: |
          echo "::error::Unknown environment param: ${{ github.event.inputs.environment }}"
          echo "::error::Allowed environments are 'dev', 'test', 'stage' and 'prod'"
