name: Deploy after data

on:
  push:
    branches: [main]

permissions:
  actions: read
  contents: read

concurrency:
  group: production-deploy
  cancel-in-progress: true

jobs:
  await-data:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Resolve the newest data-dependent change
        id: required
        shell: bash
        run: |
          set -euo pipefail
          sha="$(git log -1 --format=%H -- \
            data/production-snapshot.json \
            pipeline/parse.ts \
            pipeline/load.ts \
            src/data/read-model.ts)"
          echo "sha=$sha" >> "$GITHUB_OUTPUT"

      - name: Wait for the data pipeline to cover that change
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        shell: bash
        run: |
          scripts/wait-for-workflow-ancestor.sh \
            "Production Data Pipeline" \
            "${{ steps.required.outputs.sha }}" \
            "$GITHUB_SHA"

  deploy:
    needs: await-data
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm ci
      - run: npm test
      - run: ./scripts/deploy-production.sh "$GITHUB_SHA"
