#   === Deploy workflow ===
#
#   This workflow allows you to deploy your templates to a desired environment.
#   The outcome should be similar to running `npm run deploy` with the correct credentials.
#
#   This workflow can be triggered from the GitHub Actions tab. Just open the actions tab,
#   select the workflow, and click the "Run workflow" button. Then, select your desired
#   environment, select if you want to skip tests and confirm the deployment.
#
#   Only one deployment per environment can run at a time. If a deployment is already in
#   progress, triggering a new deployment will cancel the previous one.
#
#   ❗️ To use this, ensure you have the `CONNECT_AUTH_TOKEN` GitHub secret configured
#      with the correct auth token you used during the CLI initialization.

name: Deploy

on:
  workflow_dispatch:
    inputs:
      environment:
        description: Environment to deploy to
        required: true
        type: choice
        options:
          - development
          - qa
          - production
          - demo
      force:
        description: Skip tests during deployment
        required: false
        type: boolean
        default: false

concurrency:
  group: deploy-${{ github.event.inputs.environment }}
  cancel-in-progress: true

jobs:
  deploy:
    name: Deploy to ${{ github.event.inputs.environment }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Check secrets
        env:
          CONNECT_AUTH_TOKEN: ${{ secrets.CONNECT_AUTH_TOKEN }}
        if: env.CONNECT_AUTH_TOKEN == ''
        run: echo "Missing CONNECT_AUTH_TOKEN secret" && exit 1

      - name: Setup node
        uses: actions/setup-node@v4
        with:
          node-version: "22.18.0"

      - name: Install dependencies
        run: npm ci

      - name: Deploy
        run: npx cross-env IS_CI=true npm run deploy ${{ github.event.inputs.environment }} ${{ github.event.inputs.force && '--force' || '' }}
        env:
          CONNECT_AUTH_TOKEN: ${{ secrets.CONNECT_AUTH_TOKEN }}
