name: Deploy website to GitHub Pages

on:
  # Runs on pushes targeting the default branch
  push:
    branches: [develop]
    paths:
      - "sites/promo/**"
      - "sites/docs/**"
      - "package.json"
      - "pnpm-lock.yaml"
      - "pnpm-workspace.yaml"
      - ".github/workflows/pages.yml"

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
  contents: read
  pages: write
  id-token: write

# Allow only one concurrent deployment per the "pages" group, skipping runs queued
# between the in-progress run and the latest queued one. cancel-in-progress defaults
# to false, so in-flight production deployments are allowed to complete.
concurrency: "pages"

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install pnpm
        uses: pnpm/action-setup@v4
        with:
          run_install: false

      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "22"
          cache: "pnpm"

      # Only install the promo site (radiant) and docs site (docs-next) and their
      # dependencies so the public website deploy stays decoupled from the
      # server/desktop build pipelines.
      - name: Install dependencies
        run: pnpm install --filter radiant... --filter docs-next...

      - name: Setup Pages
        id: setup_pages
        uses: actions/configure-pages@v5

      - name: Restore cache
        uses: actions/cache@v4
        with:
          path: |
            sites/promo/.next/cache
          # Generate a new cache whenever packages or source files change.
          key: ${{ runner.os }}-nextjs-${{ hashFiles('pnpm-lock.yaml') }}-${{ hashFiles('sites/promo/**.[jt]s', 'sites/promo/**.[jt]sx') }}
          # If source files changed but packages didn't, rebuild from a prior cache.
          restore-keys: |
            ${{ runner.os }}-nextjs-${{ hashFiles('pnpm-lock.yaml') }}-

      - name: Build promo site with Next.js
        working-directory: sites/promo
        run: pnpm run build
        env:
          PAGES_BASE_PATH: ${{ steps.setup_pages.outputs.base_path }}

      - name: Build docs site with Astro
        working-directory: sites/docs
        run: pnpm run build

      # Nest the Starlight docs (built with base: "/docs") inside the promo export
      # so both ship from a single GitHub Pages deployment at /docs.
      - name: Assemble docs into /docs
        run: |
          rm -rf sites/promo/out/docs
          mkdir -p sites/promo/out/docs
          cp -r sites/docs/dist/. sites/promo/out/docs/

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: sites/promo/out

  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4
