name: docs
on:
  push:
    branches:
      - main
    paths:
      - docs
  pull_request:
    branches:
      - main
    paths:
      - docs
  workflow_dispatch:
permissions:
  contents: write
jobs:
  changed_files:
    runs-on: ubuntu-latest
    name: Review changed files
    outputs:
      docs_any_changed: ${{ steps.changed-files.outputs.docs_any_changed }}
    steps:
      - uses: actions/checkout@v4
      - name: Get changed files
        id: changed-files
        uses: tj-actions/changed-files@v45
        with:
          files_yaml: |
            docs:
              - 'docs/**'
              - 'mkdocs.yml'
          base_sha: 'main'

  documentation_validation:
    needs: changed_files
    name: Documentation validation
    if: needs.changed_files.outputs.docs_any_changed == 'true'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Remove plugins from mkdocs configuration
        run: |
          sed -i '/^plugins:/,/^[^ ]/d' mkdocs.yml
      - name: Run MkDocs build
        uses: Kjuly/mkdocs-page-builder@main

  deploy:
    runs-on: ubuntu-latest
    needs: documentation_validation
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: 3.x
      - run: echo "cache_id=$(date --utc '+%V')" >> $GITHUB_ENV 
      - uses: actions/cache@v4
        with:
          key: mkdocs-material-${{ env.cache_id }}
          path: .cache
          restore-keys: |
            mkdocs-material-
      - name: Install Python dependencies
        run: |
          pip install mkdocs-material
          pip install .[recommended,git,imaging]
          pip install mkdocs-jupyter
      - run: mkdocs gh-deploy --force
