# Cloudflare Pages Deploy Workflow
#
# Build pipeline: cargo build → assay site/build.lua → wrangler deploys build/site/
#
# PREREQUISITES:
# 1. Cloudflare Pages project 'assay-rs' with custom domain assay.rs
# 2. GitHub secrets: CLOUDFLARE_API_TOKEN, CLOUDFLARE_ACCOUNT_ID

name: Deploy to Cloudflare Pages

on:
  push:
    branches:
      - main
    paths:
      - "site/**"
      - "docs/modules/**"
      - "src/lua/builtins/mod.rs"
      - "stdlib/**"
      - "CHANGELOG.md"
      - "wrangler.toml"

concurrency:
  group: cloudflare-pages-deploy
  cancel-in-progress: false

jobs:
  deploy:
    name: Deploy site to Cloudflare Pages
    runs-on: ubuntu-latest
    permissions:
      contents: read
      deployments: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Cache cargo build
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-deploy-${{ hashFiles('Cargo.lock') }}
          restore-keys: ${{ runner.os }}-cargo-deploy-

      - name: Build assay
        run: cargo build --release

      - name: Build site (assay builds its own docs)
        run: ./target/release/assay site/build.lua

      - name: Index site for search
        run: npx --yes pagefind --site build/site

      - name: Deploy to Cloudflare Pages
        uses: cloudflare/wrangler-action@v3
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          command: pages deploy build/site/ --project-name=assay-rs
