name: Deploy v5 Preview

on:
  push:
    branches:
      - v5
  workflow_dispatch:

permissions:
  contents: read
  deployments: write

jobs:
  deploy-v5:
    if: ${{ github.repository == 'jackyzha0/quartz' }}
    runs-on: ubuntu-latest
    name: Deploy v5 to Cloudflare Pages
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Setup Node
        uses: actions/setup-node@v6
        with:
          node-version: 24

      - name: Cache dependencies
        uses: actions/cache@v5
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-

      - name: Cache Quartz plugins
        uses: actions/cache@v5
        with:
          path: .quartz/plugins
          key: ${{ runner.os }}-plugins-${{ hashFiles('quartz.lock.json') }}
          restore-keys: |
            ${{ runner.os }}-plugins-

      - run: npm ci

      - name: Install Quartz plugins
        run: npx quartz plugin install

      - name: Check types and style
        run: npm run check

      - name: Build Quartz
        run: npx quartz build -d docs -v

      - name: Deploy to Cloudflare Pages
        uses: AdrianGonz97/refined-cf-pages-action@v1
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          githubToken: ${{ secrets.GITHUB_TOKEN }}
          projectName: quartz
          deploymentName: v5 Branch Preview
          branch: v5
          directory: public
