name: Build documentation

on: push

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Install Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'
          cache: "pip"
          cache-dependency-path: |
            doc/requirements_doc.txt

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          python -m pip install -r requirements.txt
          python -m pip install git+https://github.com/tensorly/tensorly-sphinx-theme.git@main
          python -m pip install -r doc/requirements_doc.txt
      - name: Install package
        run: |
          python -m pip install -e .
      - name: Make doc
        run: |
          cd doc
          python minify.py
          make html
          cd ..
      - name: Push doc to website
        if: success() && github.ref == 'refs/heads/main'
        run: |
          # Add deploy key and clone through ssh
          eval "$(ssh-agent -s)"
          mkdir ~/.ssh
          echo "${{ secrets.DEPLOY_DOC_KEY }}" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan -t rsa github.com
          echo 'Documentation was successfully built, updating the website.'
          # See https://github.community/t/github-actions-bot-email-address/17204/5
          git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --global user.name "github-actions"
          git clone "git@github.com:tensorly/tensorly.github.io.git" doc_folder
          echo "-- Updating the content"
          cd doc_folder
          git rm -r dev/*
          mkdir -p dev
          echo "Copying to folder"
          cp -r ../doc/_build/html/* dev/
          echo "Pushing to git"
          git add dev
          git commit -m "Github action: auto-update."
          git push --force origin main
