name: Doc Build and Upload

on:
  push:
    branches:
      - main
      - 3.0.x
    tags:
      - '*'
  pull_request:
    branches:
      - main
      - 3.0.x
  workflow_dispatch:
    inputs:
      version:
        description: 'The pandas version to override'
        required: false
        type: string
      publish_prod:
        description: 'Publish to production'
        type: boolean
        default: false

env:
  ENV_FILE: environment.yml
  PANDAS_CI: 1

permissions: {}

jobs:
  web_and_docs:
    name: Doc Build and Upload
    runs-on: ubuntu-24.04
    permissions:
      contents: read

    concurrency:
      # https://github.community/t/concurrecy-not-work-for-push/183068/7
      group: ${{ github.event_name == 'push' && github.run_number || github.ref }}-web-docs
      cancel-in-progress: true

    defaults:
      run:
        shell: bash -el {0}

    steps:
    - name: Set pandas version
      env:
        INPUT_VERSION: ${{ github.event.inputs.version }}
        EVENT_NAME: ${{ github.event_name }}
        INPUT_PUBLISH_PROD: ${{ github.event.inputs.publish_prod }}
      run: |
        # tags include a `v` prefix.
        tag_version_pat="^v[0-9]+\.[0-9]+\.[0-9]+(rc[0-9]+)?$"
        version_pat="^[0-9]+\.[0-9]+\.[0-9]+(rc[0-9]+)?$"

        if [[ -n "$INPUT_VERSION" ]]; then
          PANDAS_VERSION="$INPUT_VERSION"
        elif [[ "$EVENT_NAME" == "push" && "$GITHUB_REF_NAME" =~ $tag_version_pat ]]; then
          PANDAS_VERSION="${GITHUB_REF_NAME:1}"
        else
          PANDAS_VERSION=""
        fi
        echo "PANDAS_VERSION=$PANDAS_VERSION" >> "$GITHUB_ENV"

        if [[ "$EVENT_NAME" == "push" && -n "$PANDAS_VERSION" ]]; then
          PUBLISH_PROD="true"
        elif [[ "$INPUT_PUBLISH_PROD" == "true" ]]; then
          PUBLISH_PROD="true"
        else
          PUBLISH_PROD="false"
        fi
        echo "PUBLISH_PROD=$PUBLISH_PROD" >> "$GITHUB_ENV"

        if [[ "$PUBLISH_PROD" == "true" ]] &&
           [[ ! "$PANDAS_VERSION" =~ $version_pat ]]
        then
          echo "Invalid version $PANDAS_VERSION for publishing to prod."
          exit 1
        fi

    - name: Checkout
      uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
      with:
        fetch-depth: 0

    - name: Set up Conda
      uses: ./.github/actions/setup-conda

    - name: Build Pandas
      uses: ./.github/actions/build_pandas

    - name: Extra installs
      # https://pytest-qt.readthedocs.io/en/latest/troubleshooting.html#github-actions-azure-pipelines-travis-ci-and-gitlab-ci-cd
      run: sudo apt-get update && sudo apt-get install -y libegl1 libopengl0

    - name: Test website
      run: python -m pytest web/

    - name: Build website
      run: python web/pandas_web.py web/pandas --target-path=web/build
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    - name: Build documentation
      run: doc/make.py --warnings-are-errors

    - name: Build the interactive terminal
      working-directory: web/interactive_terminal
      run: jupyter lite build

    - name: Build documentation zip
      run: doc/make.py zip_html

    - name: Install ssh key
      run: |
        mkdir -m 700 -p ~/.ssh
        echo "${{ secrets.server_ssh_key }}" > ~/.ssh/id_rsa
        chmod 600 ~/.ssh/id_rsa
        echo "${{ secrets.server_ip }} ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFjYkJBk7sos+r7yATODogQc3jUdW1aascGpyOD4bohj8dWjzwLJv/OJ/fyOQ5lmj81WKDk67tGtqNJYGL9acII=" > ~/.ssh/known_hosts
      if: (github.event_name == 'push' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/'))) || github.event_name == 'workflow_dispatch'

    - name: Copy cheatsheets into site directory
      run: cp doc/cheatsheet/Pandas_Cheat_Sheet* web/build/

    - name: Upload web
      run: rsync -az --delete --exclude='pandas-docs' --exclude='docs' --exclude='benchmarks' web/build/ web@${{ secrets.server_ip }}:/var/www/html
      if: false # github.event_name == 'push' && github.ref == 'refs/heads/main'

    - name: Upload dev docs
      run: rsync -az --delete doc/build/html/ web@${{ secrets.server_ip }}:/var/www/html/pandas-docs/dev
      if: false # github.event_name == 'push' && github.ref == 'refs/heads/main'

    - name: Upload prod docs
      run: rsync -az --delete doc/build/html/ web@${{ secrets.server_ip }}:/var/www/html/pandas-docs/version/${{ env.PANDAS_VERSION }}
      if: false # ${{ env.PUBLISH_PROD == 'true' }}

    - name: Move docs into site directory
      run: mv doc/build/html web/build/docs

    - name: Save website as an artifact
      uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7
      with:
        name: website
        path: web/build
        retention-days: 14
