name: Deploy backend to LAN dev server

# Fires on a push to `dev` touching backend/** -- a direct push, or the real
# merge commit a native "auto merge when checks succeed" produces (see
# pr-build.yml). A `build` job (the reusable build.yml sanity check) gates a
# `deploy` job that rolls the LAN dev box.
#
# CUTOVER NOTE (monorepo reunification): the LAN box's ~/thermograph-dev is a
# thermograph-infra checkout from the split era. This job calls
# ~/thermograph-dev/infra/deploy/deploy-dev.sh -- the MONOREPO layout -- so it
# is INERT until ~/thermograph-dev is re-pointed at the monorepo (a fresh
# clone; deploy-dev.sh then self-updates via deploy.sh's git reset like the
# beta/prod paths).
#
# SERVICE=backend + BACKEND_IMAGE_TAG is the same env-var contract the
# beta/prod deploy workflows use against infra/deploy/deploy.sh, so a dev-only
# rollout never touches the frontend's running container or tag.

on:
  push:
    branches: [dev]
    paths: ['backend/**']
  workflow_dispatch: {}

jobs:
  build:
    name: build
    # Fixed (unparameterized) group: if two backend pushes to dev land close
    # together, the newer push's build cancels the older's still-running one
    # on the single physical runner -- the older result would be thrown away
    # once needs:build gates its deploy anyway. Safe to cancel mid-run:
    # build.yml only runs a throwaway `docker build`, nothing persisted.
    concurrency:
      group: dev-push-build-backend
      cancel-in-progress: true
    uses: ./.forgejo/workflows/build.yml
    with:
      domain: backend

  deploy:
    needs: build
    # NOT [self-hosted, thermograph-lan] -- GitHub implicitly tags every
    # self-hosted runner with "self-hosted"; Forgejo's runner has only the
    # labels it was registered with ("docker" + "thermograph-lan"). The array
    # form silently makes this job unschedulable on every push.
    runs-on: thermograph-lan
    permissions:
      contents: read
    concurrency:
      group: dev-lan-deploy-backend
      cancel-in-progress: false
    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
        id: tag
        # Keyed to the last commit that touched backend/ -- must match
        # backend-build-push.yml's tag key exactly (see its comment).
        run: |
          domain_sha="$(git log -1 --format=%H -- backend/)"
          echo "tag=sha-${domain_sha:0:12}" >> "$GITHUB_OUTPUT"

      - name: Deploy to the LAN dev server
        # thermograph-lan is host-native (the runner job runs directly on the
        # LAN box, not over SSH like beta/prod), so plain env vars on the
        # command reach deploy-dev.sh directly -- no ssh-action needed here.
        # The lake creds ride the Actions S3 secrets: dev renders no vault
        # (deploy-dev.sh's no-op secrets path), and compose interpolates
        # ${THERMOGRAPH_LAKE_S3_*} straight from the deploy environment.
        env:
          THERMOGRAPH_LAKE_S3_ACCESS_KEY: ${{ secrets.S3_ACCESS_KEY }}
          THERMOGRAPH_LAKE_S3_SECRET_KEY: ${{ secrets.S3_SECRET_KEY }}
        run: SERVICE=backend BACKEND_IMAGE_TAG=${{ steps.tag.outputs.tag }} bash "$HOME/thermograph-dev/infra/deploy/deploy-dev.sh"
