name: "🤖 Deploy dashboard agent"

# Deploys the @internal/dashboard-agent chat.agent to its Trigger.dev project
# with --skip-promotion, so a deploy never becomes "current" on its own. The
# consuming app cuts over by pinning DASHBOARD_AGENT_VERSION to the new version.
# Runs a leg per environment (staging + prod), each gated by its own environment;
# a push to main that touches the agent or its store triggers both. Version
# numbers are per-environment, so pin each environment to its own leg's version.

on:
  push:
    branches: [main]
    paths:
      - "internal-packages/dashboard-agent/**"
      - "internal-packages/dashboard-agent-db/**"
  workflow_dispatch:

permissions: {}

jobs:
  deploy:
    name: Deploy dashboard agent (${{ matrix.environment }})
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      # One environment at a time: parallel deploys of the same project race.
      max-parallel: 1
      matrix:
        environment: [staging, prod]
    # Per-environment reviewer gate + source of the scoped deploy PAT.
    environment: dashboard-agent-${{ matrix.environment }}
    concurrency:
      group: dashboard-agent-deploy-${{ matrix.environment }}
      cancel-in-progress: false
    permissions:
      contents: read
    env:
      TRIGGER_API_URL: https://api.trigger.dev
      TRIGGER_DASHBOARD_AGENT_PROJECT_REF: ${{ vars.TRIGGER_DASHBOARD_AGENT_PROJECT_REF }}
    steps:
      - name: Checkout
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          persist-credentials: false

      - name: Setup pnpm
        uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v5.0.0
        with:
          version: 10.33.2

      - name: Setup node
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          node-version: 22.23.1
          cache: "pnpm"

      - name: Install + build the CLI and the agent's deps
        run: |
          set -euo pipefail
          pnpm install --frozen-lockfile
          # Prisma client is needed because the build closure pulls in @trigger.dev/database.
          pnpm run generate
          # Config-time imports the agent's trigger.config.ts needs: defineConfig (sdk), aptGet (build).
          pnpm run build --filter trigger.dev --filter @trigger.dev/build --filter @trigger.dev/sdk

      - name: Deploy (--skip-promotion)
        working-directory: internal-packages/dashboard-agent
        env:
          TRIGGER_ACCESS_TOKEN: ${{ secrets.TRIGGER_DASHBOARD_AGENT_DEPLOY_TOKEN }}
        # Invoke the built CLI directly (what the workspace .bin/trigger wrapper does),
        # so a not-yet-linked bin after a fresh install can't break the deploy.
        run: node ../../packages/cli-v3/dist/esm/index.js deploy --skip-promotion --env ${{ matrix.environment }}
