on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: docker
    steps:
      - uses: actions/checkout@v4

      - name: Restart Nomad allocation
        env:
          NOMAD_ADDR: ${{ secrets.NOMAD_ADDR }}
          NOMAD_TOKEN: ${{ secrets.NOMAD_TOKEN }}
        run: |
          set -e
          ALLOC_ID=$(curl -sf \
            -H "X-Nomad-Token: $NOMAD_TOKEN" \
            "$NOMAD_ADDR/v1/job/cigvillage/allocations" \
            | python3 -c "import sys,json; allocs=json.load(sys.stdin); print(next(a['ID'] for a in allocs if a['ClientStatus']=='running'))")
          echo "Stopping allocation to trigger fresh prestart: $ALLOC_ID"
          curl -sf -X POST \
            -H "X-Nomad-Token: $NOMAD_TOKEN" \
            "$NOMAD_ADDR/v1/allocation/$ALLOC_ID/stop"
          echo "Deploy triggered — Nomad will schedule a new alloc that re-fetches the site."
