# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

name: FBGEMM_GPU Documentation

on:
  # PR Trigger
  #
  pull_request:
    branches:
      - main

  # Push Trigger (enable to catch errors coming out of multiple merges)
  #
  push:
    branches:
      - main

  # Manual Trigger (for testing only)
  #
  workflow_dispatch:

jobs:
  build-docs:
    runs-on: linux.2xlarge
    container:
      image: amazonlinux:2023
      options: --user root
    defaults:
      run:
        shell: bash
    env:
      PRELUDE: .github/scripts/setup_env.bash
      BUILD_ENV: build_binary
    strategy:
      fail-fast: false
      matrix:
        python-version: [ "3.11" ]

    steps:
    - name: Setup Build Container
      run: yum update -y; yum install -y binutils findutils git pciutils rsync sudo tar wget which

    - name: Checkout the Repository
      uses: actions/checkout@v4
      with:
        submodules: true

    - name: Display System Info
      run: . $PRELUDE; print_system_info

    - name: Display GPU Info
      run: . $PRELUDE; print_gpu_info

    - name: Setup Miniconda
      run: . $PRELUDE; setup_miniconda $HOME/miniconda

    - name: Create Conda Environment
      run: . $PRELUDE; create_conda_environment $BUILD_ENV ${{ matrix.python-version }}

    - name: Install C/C++ Compilers
      run: . $PRELUDE; install_cxx_compiler $BUILD_ENV

    - name: Install Build Tools
      run: . $PRELUDE; install_build_tools $BUILD_ENV

    - name: Install Documentation Tools
      run: . $PRELUDE; install_docs_tools $BUILD_ENV

    - name: Install PyTorch-CPU Nightly
      run: . $PRELUDE; install_pytorch_pip $BUILD_ENV nightly cpu

    - name: Prepare FBGEMM_GPU Build
      run: . $PRELUDE; cd fbgemm_gpu; prepare_fbgemm_gpu_build $BUILD_ENV

    - name: Build + Install FBGEMM_GPU (CPU version)
      run: . $PRELUDE; cd fbgemm_gpu; build_fbgemm_gpu_install $BUILD_ENV cpu

    - name: Build FBGEMM_GPU Documentation
      run: . $PRELUDE; cd fbgemm_gpu/docs; build_fbgemm_gpu_docs $BUILD_ENV

    - name: Deploy FBGEMM_GPU Documentation
      if: ${{ github.event_name != 'pull_request' }}
      uses: JamesIves/github-pages-deploy-action@releases/v4
      with:
        branch: gh-pages                    # The branch the action should deploy to
        folder: fbgemm_gpu/docs/build/html  # The folder the action should deploy
