name: Deploy Docs Site
permissions:
  contents: read
on:
  push:
    branches: ['main']
  pull_request:
  workflow_dispatch:

jobs:
  build:
    name: Build Docusaurus
    runs-on: ubuntu-latest
    permissions:
      contents: read
      actions: write
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-node@v6
        with:
          node-version: 20
          cache: yarn
          cache-dependency-path: |
            yarn.lock
            website/yarn.lock
      - name: yarn install (root)
        run: yarn install --immutable
      - name: Cache for Turbo
        uses: rharkor/caching-for-turbo@v2.3.11
        with:
          cache-prefix: turbo-cache-
      - name: Cache for Turbo
        uses: rharkor/caching-for-turbo@v2.3.11
        with:
          cache-prefix: turbo-cache-website-
          server-port: 41231 # The default is 41230, but we use a different cache key so we need a different port
      - name: Build all pkg
        run: yarn release-build
      - name: Build docs
        run: yarn turbo build:doc --single-package
      - run: yarn install --immutable
        working-directory: ./website
      - name: Restore bundler cache
        uses: actions/cache@v5
        with:
          key: ${{ runner.os }}-docusaurus-bundler-${{ github.sha }}
          path: ./website/node_modules/.cache
          restore-keys: |
            ${{ runner.os }}-docusaurus-bundler-
      - name: Build website
        run: yarn turbo build
        working-directory: ./website
        env:
          TURBO_API: http://localhost:41231
      - name: Upload artifact
        uses: actions/upload-pages-artifact@v4
        with:
          path: ./website/build
  typecheck:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-node@v6
        with:
          node-version: 20
          cache: yarn
          cache-dependency-path: |
            yarn.lock
            website/yarn.lock
      - name: yarn install (root)
        run: yarn install --immutable
      - name: Cache for Turbo
        uses: rharkor/caching-for-turbo@v2.3.11
        with:
          cache-prefix: turbo-cache-
      - name: Build all pkg
        run: yarn release-build
      - name: Build docs
        run: yarn turbo build:doc --single-package
      - run: yarn install --immutable
        working-directory: ./website
      - run: yarn typecheck
        working-directory: ./website
  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    needs: build
    if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
    concurrency: deploy-site
    permissions:
      pages: write
      id-token: write
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v5
