name: Update gh-pages documentation

on:
  workflow_dispatch:
  push:
    branches: [main]

jobs:
  update-docs:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-python@v5
      with:
        python-version: '3.11'
        cache: 'pip'
        cache-dependency-path: 'docs/requirements.txt'
    - run: |
        python -m pip install -r docs/requirements.txt
        python -m pip install .
    - run: |
        python -m sphinx -b html -d docs/build/doctrees docs/source docs/build/html
    - name: Check out gh-pages branch
      run: |
        git fetch --depth=1 origin gh-pages
        git worktree add ../docs gh-pages
    - name: Update the docs on the gh-pages branch
      run: |
        python docs/update_gh_pages.py docs/build/html ../docs
    - name: Configure Git
      run: |
        git config user.name "Documentation Bot"
        git config user.email "info@enthought.com"
    - name: Commit the changes
      run: |
        cd ../docs
        git add .
        # Only commit if there are changes
        git diff-index --quiet --cached HEAD || git commit -m "Automated update of main branch documentation"
    - name: Push the changes upstream
      run: |
        git push origin gh-pages
