name: Publish Python API Docs

# Run when the Python API changes or every month so that the artifact does not expire
on:
  push:
    branches:
      - main
    paths:
      - onnxruntime_extensions/**
      - docs/python/**
  schedule:
    - cron: '0 0 1 * *'
  workflow_dispatch:

concurrency:
  group: "apidocs-python"
  cancel-in-progress: true

permissions:
  contents: write

jobs:
  build:
    name: Generate Python API docs
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install tools
        run: |
          sudo apt-get update
          sudo apt-get install python3-pip
      - name: Install dependencies
        run: |
          python3 -m pip install --upgrade pip
          python3 -m pip install -r requirements-dev.txt
          python3 -m pip install -e .
          python3 -m pip list
      - name: Generate Python docs with Sphinx
        run: |
          set -e -x
          python3 -m pip install -r docs/python/requirements.txt
          rm -rf sphinx_output/docs/
          python3 -m sphinx -j1 -v -T -b html -d sphinx_output/docs/extensions/_doctrees/html docs/python/ sphinx_output/docs/extensions/html
      - name: Log source commit
        run: |
          mkdir -p sphinx_output/docs/extensions/html/
          git rev-parse --short HEAD >> sphinx_output/docs/extensions/html/source-version.txt
      - name: Move Python docs into site
        run: |
          rm -rf _site/docs/api/python
          mkdir -p _site/docs/api/
          mv sphinx_output/docs/extensions/html _site/docs/api/python
      - name: Upload docs artifact
        uses: actions/upload-artifact@v3
        with:
          name: onnxruntime-extensions-python-apidocs
          path: _site
          retention-days: 60