name: Build documentation

on:
  pull_request:
  push:
    branches:
      - nightly
      - main
      - release/*
    tags:
      - v[0-9]+.[0-9]+.[0-9]
      - v[0-9]+.[0-9]+.[0-9]+-rc[0-9]+
  workflow_dispatch:

jobs:
  build:
    strategy:
      matrix:
        python_version: ["3.10"]
        cuda_arch_version: ["11.8"]
      fail-fast: false
    uses: pytorch/test-infra/.github/workflows/linux_job.yml@main
    with:
      job-name: Build doc
      runner: linux.g5.4xlarge.nvidia.gpu
      repository: pytorch/audio
      gpu-arch-type: cuda
      gpu-arch-version: ${{ matrix.cuda_arch_version }}
      timeout: 120
      upload-artifact: docs

      script: |
        set -ex
        # Set up Environment Variables
        export PYTHON_VERSION="${{ matrix.python_version }}"
        export CU_VERSION="${{ matrix.cuda_arch_version }}"
        export CUDATOOLKIT="pytorch-cuda=${CU_VERSION}"
        export BUILD_VERSION="$( cut -f 1 -d a version.txt )".dev"$(date "+%Y%m%d")"

        # Set CHANNEL
        if [[(${GITHUB_EVENT_NAME} = 'pull_request' && (${GITHUB_BASE_REF} = 'release'*)) || (${GITHUB_REF} = 'refs/heads/release'*) ]]; then
          export CHANNEL=test
        else
          export CHANNEL=nightly
        fi

        echo "::group::Create conda env"
        # Mark Build Directory Safe
        git config --global --add safe.directory /__w/audio/audio
        conda create --quiet -y --prefix ci_env python="${PYTHON_VERSION}"
        conda activate ./ci_env

        echo "::endgroup::"
        echo "::group::Install PyTorch"
        conda install \
          --yes \
          --quiet \
          -c "pytorch-${CHANNEL}" \
          -c nvidia "pytorch-${CHANNEL}"::pytorch[build="*${CU_VERSION}*"] \
          "${CUDATOOLKIT}"

        echo "::endgroup::"
        echo "::group::Install TorchAudio"
        conda install --quiet --yes pkg-config cmake>=3.18.0 ninja
        pip3 install --progress-bar off -v -e . --no-use-pep517

        echo "::endgroup::"
        echo "::group::Build FFmpeg"
        .github/scripts/ffmpeg/build_gpu.sh

        echo "::endgroup::"
        echo "::group::Install other dependencies"
        conda install \
          --quiet --yes \
          -c conda-forge \
          pandoc doxygen pysoundfile
        pip install --progress-bar off \
            git+https://github.com/kpu/kenlm/ flashlight-text \
            -r docs/requirements.txt -r docs/requirements-tutorials.txt

        echo "::endgroup::"
        echo "::group::Build documentation"
        export BUILD_GALLERY=true
        (cd docs && make html)

        echo "::endgroup::"
        echo "::group::Copy artifact"
        cp -rf docs/build/html/* "${RUNNER_DOCS_DIR}"
        mv docs/build/html /artifacts/

  commit:
    if: ${{ (github.repository == 'pytorch/audio') && ((github.event_name == 'push') && (github.ref_name == 'nightly')) }}
    permissions:
      # Required for `git push`
      # Note:
      #   This is not effective from fork.
      #   When you debug this, make sure to make a branch on pytorch and
      #   make PR from there.
      contents: write
    runs-on: ubuntu-latest
    needs: build
    steps:
    - uses: actions/checkout@v3
      with:
        ref: gh-pages
        fetch-depth: 5
    - uses: actions/download-artifact@v3
      with:
        name: docs
    - name: Update nightly doc
      run: |
        set -x

        git config user.name "pytorchbot"
        git config user.email "soumith+bot@pytorch.org"
        # When `git clone`, `gh-pages` branch is fetched by default.
        # The size of gh-pages grows significantly, so we use ammend and force push
        # We add a new commit once a week
        if [ "date +%u" = "1" ]; then
           git commit --allow-empty -m "placeholder"
        fi

        # TODO: add tag-based process (need to handle the main directory name)
        # Update the main doc
        rm -rf main
        mv html main
        git add --all main || true

        git commit --amend -m "auto-generating sphinx docs" || true
        git push -f
