name: Deploy docs

on:
  push:
    branches:
      - main
    paths:
      - "docs/**"
      - "mkdocs.yml"

permissions:
  contents: write # This allows pushing to gh-pages

jobs:
  deploy_docs:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0
      - name: Determine docs-only push
        id: docs-only
        run: |
          if [ "${{ github.event_name }}" != "push" ]; then
            echo "skip=false" >> "$GITHUB_OUTPUT"
            exit 0
          fi
          set -euo pipefail
          before="${{ github.event.before }}"
          sha="${{ github.sha }}"
          changed_files=$(git diff --name-only "$before" "$sha" || true)
          non_docs=$(echo "$changed_files" | grep -vE '^(docs/|mkdocs.yml$)' || true)
          if [ -n "$non_docs" ]; then
            echo "skip=true" >> "$GITHUB_OUTPUT"
          else
            echo "skip=false" >> "$GITHUB_OUTPUT"
          fi
      - name: Setup uv
        if: steps.docs-only.outputs.skip != 'true'
        uses: astral-sh/setup-uv@fac544c07dec837d0ccb6301d7b5580bf5edae39 # setup-uv v8.1.0; uv 0.11.14
        with:
          version: "0.11.14"
          enable-cache: true
      - name: Install dependencies
        if: steps.docs-only.outputs.skip != 'true'
        run: make sync
      - name: Deploy docs
        if: steps.docs-only.outputs.skip != 'true'
        run: make deploy-docs
