name: Build and Deploy Online Documentation

on:
  # Trigger the workflow on push request,
  # for the dev branch
  push:
    branches:
      - 'dev'
    paths:
     - 'docs/**'
     - 'lmcache/**'
     - 'examples/**'
     - '.github/workflows/build_doc.yml'
jobs:
  build-docs:
    name: Build docs
    runs-on: ubuntu-latest
    steps:
      - name: Harden Runner
        uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
        with:
          egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs

      - name: Checkout code
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          # Full git history needed for sphinx-multiversion to detect all tags/branches
          fetch-depth: 0
          # Fetch all branches and tags
          fetch-tags: true

      - name: Setup Python 3.13
        uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
        with:
          python-version: "3.13"

      - name: Install Dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements/docs.txt

      - name: Build Versioned Sphinx Documentation
        run: |
          sphinx-multiversion docs/source output
        continue-on-error: false

      - name: Setup root docs and .nojekyll
        run: |
          touch output/.nojekyll
          # Copy dev docs to root (latest docs at root URL)
          cp -r output/dev/* output/
          # Remove dev directory to avoid duplication
          rm -rf output/dev

      - name: Upload doc artifacts to GHA
        uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
        with:
          name: doc-artifacts
          path: output/

  deploy-docs:
    name: Deploy docs online
    runs-on: ubuntu-latest
    needs: build-docs
    steps:
      - name: Harden Runner
        uses: step-security/harden-runner@20cf305ff2072d973412fa9b1e3a4f227bda3c76 # v2.14.0
        with:
          egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs

      - name: Fetch doc artifacts
        uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0
        with:
            name: doc-artifacts
            path: output

      - name: Checkout code
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          repository: LM-Cache-Website/lm-cache-website.github.io
          token: ${{ secrets.LMCACHE_DOC }}
          path: target-repo

      - name: Copy Files to Target Repository
        run: |
          cp -r output/* target-repo/

      - name: Commit and Push to Target Repository
        run: |
          cd target-repo
          git config user.name "github-actions[bot]"
          git config user.email "github-actions@github.com"
          git add .
          if ! git diff --cached --quiet; then
            git commit -m "Deploy updated online docs"
            git push https://x-access-token:${{ secrets.LMCACHE_DOC }}@github.com/LM-Cache-Website/lm-cache-website.github.io.git main --force
          else
            echo "No changes to commit."
          fi
