name: Deploy 📦 to Test-Pypi and Pypi 🐍

on:
  push:
    tags:
    - '*'

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'
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          python -m pip install -r requirements.txt
          python -m pip install -r doc/requirements_doc.txt
          pip install setuptools wheel
      - name: Install package
        run: |
          python -m pip install -e .
      - name: Make doc
        run: |
          cd doc
          python minify.py
          make html
          cd ..
      - name: Build a binary wheel and a source tarball
        run: |
          python setup.py sdist bdist_wheel
      - name: Store the distribution packages
        uses: actions/upload-artifact@v4
        with:
          name: python-package-distributions
          path: dist/

  publish-to-testpypi:
    name: Publish Python 🐍 distribution 📦 to TestPyPI
    needs:
    - build
    runs-on: ubuntu-latest

    environment:
      name: testpypi
      url: https://test.pypi.org/p/tensorly

    permissions:
      id-token: write

    steps:
    - name: Download all the dists
      uses: actions/download-artifact@v4
      with:
        name: python-package-distributions
        path: dist/
    - name: Publish distribution 📦 to TestPyPI
      uses: pypa/gh-action-pypi-publish@release/v1
      with:
        repository-url: https://test.pypi.org/legacy/

  publish-to-pypi:
    name: >-
      Publish Python 🐍 distribution 📦 to PyPI
    if: startsWith(github.ref, 'refs/tags/')  # only publish to PyPI on tag pushes
    needs:
    - build
    runs-on: ubuntu-latest
    environment:
      name: pypi
      url: https://pypi.org/p/tensorly  # Replace <package-name> with your PyPI project name
    permissions:
      id-token: write  # IMPORTANT: mandatory for trusted publishing

    steps:
    - name: Download all the dists
      uses: actions/download-artifact@v4
      with:
        name: python-package-distributions
        path: dist/
    - name: Publish distribution 📦 to PyPI
      uses: pypa/gh-action-pypi-publish@release/v1
    - name: Push doc to stable if upload was successful
      if: success()
      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
        cp -r dev/* stable/
        echo "Pushing to git"
        git add stable
        git commit -m "Github action: new release."
        git push --force origin main
