# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Build Docs

on: 
  pull_request:
    branches: [ master ]

jobs:
  docs:
    runs-on: self-hosted
    steps:
      - uses: actions/checkout@v2
      - uses: seanmiddleditch/gha-setup-ninja@master

      - name: Set PR Number
        uses: actions/github-script@0.3.0
        with:
          github-token: ${{github.token}}
          script: |
            const core = require('@actions/core')
            const prNumber = context.payload.number;
            core.exportVariable('PULL_NUMBER', prNumber);
            core.exportVariable("PATH", "/home/ubuntu/anaconda3/bin:/usr/local/bin:/usr/bin/:/bin:$PATH")
        
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install numpy -I
          pip install pytest torch
          
      - name: Install package
        run: |
          pip install -e .
          
      - name: Install Sphix Dependencies
        run: |
          cd docs/
          pip install -r requirements.txt
          
      - name: Build Sphinx docs
        run: |
          cd docs/
          make html
          touch build/html/.nojekyll
          aws s3 sync build/html/ s3://hangzh/encoding/docs/${{ env.PULL_NUMBER }}/ --acl public-read --follow-symlinks --delete
          
      - name: Comment
        if: success()
        uses: thollander/actions-comment-pull-request@master
        with:
          message: "The docs are uploaded and can be previewed at http://hangzh.s3.amazonaws.com/encoding/docs/${{ env.PULL_NUMBER }}/index.html"
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
