name: Build Docs
on:
  push:
    branches:
      - master
  pull_request:
    types: [opened, synchronize]
jobs:
  build-docs:
    runs-on: ubuntu-latest
    steps:
      - name: Dump GitHub context
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
        run: echo "$GITHUB_CONTEXT"
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: "3.11"
      - uses: actions/cache@v3
        id: cache
        with:
          path: ${{ env.pythonLocation }}
          key: ${{ runner.os }}-python-docs-${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml') }}-v03
      - name: Install docs extras
        if: steps.cache.outputs.cache-hit != 'true'
        run: pip install .[doc]
      - name: Install Material for MkDocs Insiders
        if: ( github.event_name != 'pull_request' || github.event.pull_request.head.repo.fork == false ) && steps.cache.outputs.cache-hit != 'true'
        run: pip install git+https://${{ secrets.ACTIONS_TOKEN }}@github.com/squidfunk/mkdocs-material-insiders.git
      - name: Build Docs
        run: python ./scripts/docs.py build-all
      - name: Zip docs
        run: bash ./scripts/zip-docs.sh
      - uses: actions/upload-artifact@v3
        with:
          name: docs-zip
          path: ./site/docs.zip
      - name: Deploy to Netlify
        uses: nwtgck/actions-netlify@v2.0.0
        with:
          publish-dir: './site'
          production-branch: master
          github-token: ${{ secrets.GITHUB_TOKEN }}
          enable-commit-comment: false
        env:
          NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
          NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
