# Nightly release workflow for SGLang Router

name: Nightly Release SGLang Router to PyPI

on:
  schedule:
    # Run at 2 AM UTC every day
    - cron: '0 2 * * *'
  workflow_dispatch:  # Allow manual trigger

jobs:
  build:
    name: Build on ${{ matrix.os }} (${{ matrix.target }})
    runs-on: ${{ matrix.os }}-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu
            target: x86_64

    steps:
      - uses: actions/checkout@v4
        with:
          path: sglang-repo

      - name: Move sgl-router folder to root and delete sglang-repo
        run: |
          mv sglang-repo/sgl-router/* .
          rm -rf sglang-repo
          ls -alt

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"

      - name: Modify version for nightly release
        run: |
          # Get current version from pyproject.toml
          CURRENT_VERSION=$(python -c "import tomllib; print(tomllib.load(open('pyproject.toml', 'rb'))['project']['version'])" 2>/dev/null || python -c "import tomli; print(tomli.load(open('pyproject.toml', 'rb'))['project']['version'])")
          # Create nightly version with date: e.g., 0.1.9.dev20250112
          NIGHTLY_VERSION="${CURRENT_VERSION}.dev$(date +%Y%m%d)"
          echo "Nightly version: $NIGHTLY_VERSION"

          # Update pyproject.toml with nightly version (temporary, not committed)
          sed -i "s/version = \"${CURRENT_VERSION}\"/version = \"${NIGHTLY_VERSION}\"/" pyproject.toml

          # Verify the change
          cat pyproject.toml | grep "^version"

      - name: Install build dependencies
        run: |
          python -m pip install -U pip
          python -m pip install build twine auditwheel tomli

      - name: Build package
        uses: pypa/cibuildwheel@v2.21.3
        env:
          CIBW_BUILD: "cp38-manylinux_x86_64 cp39-manylinux_x86_64 cp310-manylinux_x86_64 cp311-manylinux_x86_64 cp312-manylinux_x86_64"
          CIBW_BEFORE_ALL: |
            yum update -y && yum install -y openssl-devel wget unzip && \
            # Install latest protoc (v32.0) that supports proto3
            cd /tmp && \
            wget https://github.com/protocolbuffers/protobuf/releases/download/v32.0/protoc-32.0-linux-x86_64.zip && \
            unzip protoc-32.0-linux-x86_64.zip -d /usr/local && \
            rm protoc-32.0-linux-x86_64.zip && \
            # Install Rust
            curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
          CIBW_ENVIRONMENT: "PATH=$HOME/.cargo/bin:$PATH"

      - name: List built packages
        run: ls -lh wheelhouse/

      - name: Check packages
        run: twine check --strict wheelhouse/*

      - uses: actions/upload-artifact@v4
        with:
          name: packages-${{ matrix.os }}-${{ matrix.target }}
          path: wheelhouse/

  build-sdist:
    name: Build SDist
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          path: sglang-repo

      - name: Move sgl-router folder to root, copy the license file, and delete sglang-repo
        run: |
          mv sglang-repo/sgl-router/* .
          mv sglang-repo/LICENSE .
          rm -rf sglang-repo
          ls -alt

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"

      - name: Modify version for nightly release
        run: |
          # Get current version from pyproject.toml
          CURRENT_VERSION=$(python -c "import tomllib; print(tomllib.load(open('pyproject.toml', 'rb'))['project']['version'])" 2>/dev/null || python -c "import tomli; print(tomli.load(open('pyproject.toml', 'rb'))['project']['version'])")
          # Create nightly version with date: e.g., 0.1.9.dev20250112
          NIGHTLY_VERSION="${CURRENT_VERSION}.dev$(date +%Y%m%d)"
          echo "Nightly version: $NIGHTLY_VERSION"

          # Update pyproject.toml with nightly version (temporary, not committed)
          sed -i "s/version = \"${CURRENT_VERSION}\"/version = \"${CURRENT_VERSION}\"/g" pyproject.toml
          sed -i "0,/version = \"${CURRENT_VERSION}\"/s//version = \"${NIGHTLY_VERSION}\"/" pyproject.toml

          # Verify the change
          cat pyproject.toml | grep "^version"

      - name: Build SDist
        run: |
          pip install build
          python -m pip install -U packaging tomli
          python -m build --sdist

      - uses: actions/upload-artifact@v4
        with:
          name: sdist
          path: dist/*.tar.gz

  upload:
    name: Upload to TestPyPI
    if: github.repository == 'sgl-project/sglang'  # Ensure this job only runs for the sgl-project/sglang repository
    needs: [build, build-sdist]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v4
        with:
          path: dist
          merge-multiple: true

      - name: Upload to TestPyPI
        env:
          TWINE_USERNAME: __token__
          TWINE_PASSWORD: ${{ secrets.TEST_PYPI_TOKEN_ROUTER }}
        run: |
          pip install twine
          twine upload --repository testpypi dist/* --verbose
