name: Deploy frontend to prod VPS

# On a push to `release` that touches frontend/**, SSH to prod and roll ONLY
# the frontend service onto the image frontend-build-push.yml published for this
# commit. Same shape as frontend-deploy.yml (the `main`->beta path) against a
# completely separate secret set (PROD_SSH_HOST/USER/KEY/PORT) so a beta
# credential leak can't reach prod. Backend deploys itself independently via
# backend-deploy-prod.yml -- a frontend change ships to prod without touching
# backend and vice versa, exactly as in the split era.
#
# The tag MUST match frontend-build-push.yml's last-frontend-commit key (12
# hex) -- see frontend-deploy.yml for why it's truncated here. deploy.sh
# retries the pull because the build for this push may still be in flight.

#
# Frontend's own register() fetches the IndexNow key from backend at boot, so
# deploy.sh waits on a healthy backend before declaring the frontend roll OK
# (its compose depends_on already encodes that ordering).

on:
  push:
    branches: [release]
    paths: ['frontend/**']
  workflow_dispatch: {}

concurrency:
  group: prod-deploy-frontend
  cancel-in-progress: false

jobs:
  deploy:
    runs-on: docker
    steps:
      - uses: actions/checkout@v4
        # fetch-depth 0: the tag is keyed to the LAST COMMIT THAT TOUCHED THIS
        # DOMAIN, not the branch tip -- in a path-filtered monorepo the tip is
        # often an unrelated domain's commit, and a depth-1 clone can't see
        # past it to find the real key.
        with:
          fetch-depth: 0

      - name: Compute image tag (last frontend-touching commit)
        id: tag
        run: |
          domain_sha="$(git log -1 --format=%H -- frontend/)"
          echo "tag=sha-${domain_sha:0:12}" >> "$GITHUB_OUTPUT"

      - name: Deploy frontend over SSH
        uses: https://github.com/appleboy/ssh-action@v1.2.0
        with:
          host: ${{ secrets.PROD_SSH_HOST }}
          username: ${{ secrets.PROD_SSH_USER }}
          key: ${{ secrets.PROD_SSH_KEY }}
          port: ${{ secrets.PROD_SSH_PORT }}
          # Forward the target service + the image tag to run. deploy.sh reads
          # FRONTEND_IMAGE_TAG and rolls just `frontend`.
          envs: SERVICE,FRONTEND_IMAGE_TAG
          script: /opt/thermograph/infra/deploy/deploy.sh
        env:
          SERVICE: frontend
          FRONTEND_IMAGE_TAG: ${{ steps.tag.outputs.tag }}
