name: Covasim Docs workflow
on:
  push:
    branches:
      - master

jobs:
  docs:
    runs-on: ubuntu-latest
    if: github.repository == 'InstituteforDiseaseModeling/covasim'
    strategy:
      fail-fast: false
      max-parallel: 8
      matrix:
        python-version: ['3.7']
    name: Install and Generate Docs
    steps:
      - name: Set repo owner env variable
        shell: python
        run: print("::set-env name=GITHUB_OWNER::{}".format('${{github.repository}}'.split('/')[0]))
      - name: Set repo owner env variable
        shell: python
        run: print("::set-env name=GITHUB_OWNER::{}".format('${{github.repository}}'.split('/')[0]))
      - name: Set PYTHONPATH
        run: |
          mkdir -p $HOME/.cache/site-packages
          echo "::set-env name=PYTHONPATH::$HOME/.cache/site-packages"
      - name: Checkout Docs Repo
        uses: actions/checkout@v2
        with:
          repository: ${{ env.GITHUB_OWNER }}/covasim-docs
          path: covasim-docs
          ssh-key: '${{ secrets.DOCS_DEPLOY_KEY }}'
      - name: Checkout sources
        uses: actions/checkout@v2
        with:
          path: covasim
      - uses: actions/setup-python@master
        with:
          python-version: ${{ matrix.python-version }}
          architecture: x64
      - name: Cache Packages
        id: cache-packages
        uses: actions/cache@v1
        with:
          path: ~/.cache/ # This path is specific to Ubuntu
          key: docs-packages-${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('requirements.txt') }}--${{ hashFiles('docs/requirements.txt') }} # Look to see if there is a cache hit for the corresponding requirements file
          restore-keys: |
            docs-packages-${{ runner.os }}-${{ matrix.python-version }}- # Pip install first will make setup.py much faster and the cache will make pip install fast
      - name: Install Covasim
        run: |
          pushd covasim
          python setup.py develop --install-dir ~/.cache/site-packages
          pip3 install -r docs/requirements.txt
          popd
      - name: Build Docs
        run: |
          pushd covasim/docs
          make html
          popd

          mv covasim-docs/covasim.pdf ./covasim.pdf
          rm -rf covasim-docs/*
          rsync -r covasim/docs/_build/html/ covasim-docs/
          mv ./covasim.pdf covasim-docs/covasim.pdf
      - name: Commit Docs
        run: |
          pushd covasim-docs
          if [ ! -z "$(git status --porcelain)" ]; then
            git config --local user.email "action@github.com"
            git config --local user.name "GitHub Action"
            git add --all .
            git commit -m "Update Docs"
            
            # Setup deploy key
            eval "$(ssh-agent -s)"
            ssh-add - <<< "${{ secrets.DOCS_DEPLOY_KEY }}"

            # Deploy
            remote_repo="git@github.com:${{ env.GITHUB_OWNER }}/covasim-docs.git"
            git push "${remote_repo}" HEAD:${{ github.ref }} --follow-tags
          fi
          popd
