name: Build documentation

on:
  push:
    branches:
      - master
      - doc-builder*

jobs:
  build_and_package:
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash -l {0}

    steps:
      - uses: actions/checkout@v2
        with:
          repository: 'huggingface/doc-builder'
          path: doc-builder
          token: ${{ secrets.HUGGINGFACE_PUSH }}
      
      - uses: actions/checkout@v2
        with:
          repository: 'huggingface/transformers'
          path: transformers
      
      - uses: actions/checkout@v2
        with:
          repository: 'huggingface/notebooks'
          path: notebooks
          token: ${{ secrets.HUGGINGFACE_PUSH }}
      
      - name: Loading cache.
        uses: actions/cache@v2
        id: cache
        with:
          path: ~/.cache/pip
          key: v1-test_build_doc
          restore-keys: |
            v1-test_build_doc-${{ hashFiles('setup.py') }}
            v1-test_build_doc

      - name: Setup environment
        run: |
          sudo apt-get -y update && sudo apt-get install -y libsndfile1-dev

          pip install git+https://github.com/huggingface/doc-builder@add_install_cell
          pip install git+https://github.com/huggingface/transformers#egg=transformers[dev]

          export TORCH_VERSION=$(python -c "from torch import version; print(version.__version__.split('+')[0])")
          pip install torch-scatter -f https://data.pyg.org/whl/torch-${TORCH_VERSION}+cpu.html

          pip install torchvision
          python -m pip install 'git+https://github.com/facebookresearch/detectron2.git'

          sudo apt install tesseract-ocr
          pip install pytesseract
          pip install pytorch-quantization --extra-index-url https://pypi.ngc.nvidia.com
          pip install https://github.com/kpu/kenlm/archive/master.zip

      - name: Setup git
        run: |
          git config --global user.name "Hugging Face"
          git config --global user.email transformers@huggingface.co
          
          cd doc-builder
          git pull origin main
          cd ..

          cd notebooks
          git pull origin master
          cd ..

      - name: Make documentation
        run: |
          doc-builder build transformers transformers/docs/source --build_dir doc-builder/build --notebook_dir notebooks/transformers_doc --clean

      - name: Push to repositories
        run: |
          cd doc-builder
          if [[ `git status --porcelain` ]]; then
            git add build
            git commit -m "Updated with commit ${{ github.sha }}"
            git push origin main
          else
            echo "No diff in the documentation."
          fi
          cd ..

          cd notebooks
          if [[ `git status --porcelain` ]]; then
            git add transformers_doc
            git commit -m "Updated Transformer doc notebooks with commit ${{ github.sha }}"
            git push origin master
          else
            echo "No diff in the notebooks."
          fi
          cd ..
