name: Build Site

on:
  pull_request:
  push:
    branches: [main]

permissions:
  contents: read

concurrency:
  group: site-build-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  build:
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}

      - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          node-version: "22"
          cache: npm
          cache-dependency-path: |
            package-lock.json
            website/package-lock.json

      - name: Install JS dependencies
        run: npm ci

      - name: Build admin SPA
        run: npm run build

      - name: Build documentation site
        working-directory: website
        run: npm ci && npm run build

      - name: Prepare deploy bundle
        run: |
          set -euo pipefail
          mkdir -p _bundle/public
          cp web/public/index.html _bundle/public/index.html
          cp web/public/graph.html _bundle/public/graph.html
          mkdir -p _bundle/public/assets
          cp -a web/dist/assets/. _bundle/public/assets/
          mkdir -p _bundle/public/admin
          cp -a web/dist/admin/. _bundle/public/admin/
          mkdir -p _bundle/public/docs
          cp -a website/build/. _bundle/public/docs/
          mkdir -p _bundle/worker
          cp -a cloudflare_site/worker/. _bundle/worker/

      - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: site
          path: _bundle/
          retention-days: 5
