name: Deploy PAC Tester to GitHub Pages

on:
  push:
    branches: [main]
    paths:
      - "web/**"
      - "src/pac_utils.h"
      - "src/pac_utils_dump.c"
      - ".github/workflows/deploy-pages.yml"
  workflow_dispatch:

permissions:
  contents: read
  pages: write
  id-token: write

# Only one Pages deployment at a time. Cancel any in-progress run when a
# newer one starts, preventing the "multiple github-pages artifacts" error
# that occurs when two runs both reach the upload step concurrently.
concurrency:
  group: pages
  cancel-in-progress: true

jobs:
  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

      # Regenerate web/pac_utils.js from src/pac_utils.h so the deployed
      # site is always in sync even if someone forgot to run
      # `make -C src pac_utils_js` locally before committing.
      - name: Regenerate pac_utils.js
        run: make -C src pac_utils_js

      - name: Copy API docs to web/api
        run: |
          mkdir -p web/api
          cp -a docs/html/* web/api/

      - uses: actions/configure-pages@v6

      - uses: actions/upload-pages-artifact@v4
        with:
          path: web/

      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v5
