name: Docs

on:
  push:
    branches: [main]
  workflow_dispatch:

# Needed for the official GH Pages deploy action to mint its OIDC token.
permissions:
  contents: read
  pages: write
  id-token: write

# Only one deploy at a time; superseded runs get cancelled so a rapid-fire push
# sequence doesn't leave stale deploys racing for the live site.
concurrency:
  group: pages
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: Build site
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2

      - name: Generate reference pages
        run: cargo xtask

      - uses: actions/setup-node@v6
        with:
          node-version: 20
          cache: npm
          cache-dependency-path: docs-site/package-lock.json

      - name: Install docs-site deps
        working-directory: docs-site
        run: npm ci

      - name: Build Astro site
        working-directory: docs-site
        run: npm run build

      - uses: actions/configure-pages@v6
      - uses: actions/upload-pages-artifact@v5
        with:
          path: docs-site/dist

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