name: Deploy docs

on:
  push:
    branches:
      - stable

jobs:
  docs:
    runs-on: ubuntu-latest
    steps:
      # <common-build> - Uses YAML anchors in the future
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: 18
      - name: Node.js version
        id: node
        run: echo "v8CppApiVersion=$(node --print "process.versions.modules")" >> $GITHUB_OUTPUT
      - name: Restore dependencies
        uses: actions/cache@master
        id: cache-deps
        with:
          path: |
            node_modules
            packages/*/node_modules
          key: ${{ runner.os }}-${{ steps.node.outputs.v8CppApiVersion }}-${{ hashFiles('**/yarn.lock', '**/package.json') }}
      - name: Install & build
        if: steps.cache-deps.outputs.cache-hit != 'true'
        run: yarn install --frozen-lockfile --ignore-optional && yarn build
      - name: Build
        run: yarn build
        if: steps.cache-deps.outputs.cache-hit == 'true'
      # </common-build>

      - name: Build and collect docs
        run: yarn build:docs

      - name: Set up Python
        uses: actions/setup-python@v1
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r docs/requirements.txt
      - name: Build docs
        run: mkdocs build --site-dir site -v --clean

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./site
