name: Nightly Build
on:
  schedule:
    - cron: '0 8 * * *' # UTC time
  workflow_dispatch:
permissions:
  contents: write
jobs:
  tag:
    name: Check if main branch is a dev version
    runs-on: ubuntu-latest
    if: github.repository == 'nunchaku-tech/nunchaku'
    outputs:
      need_build: ${{ steps.check.outputs.need_build }}
      tag_name: ${{ steps.tag.outputs.tag_name }}
    steps:
      - name: Checkout main branch
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          ref: main
      - name: Extract version from __version__.py
        id: version
        run: |
          version=$(grep '__version__' nunchaku/__version__.py | sed -E 's/.*"([^"]+)".*/\1/')
          echo "Extracted version: $version"
          echo "version=$version" >> "$GITHUB_OUTPUT"
      - name: Determine if build is needed
        id: check
        run: |
          version="${{ steps.version.outputs.version }}"
          need_build=false
          if [[ "$version" == *dev* ]]; then
            echo "Version contains 'dev'"
            prefix="v$version"
            tag=$(git tag --list "${prefix}*" --sort=-creatordate | head -n 1 || echo "")
            if [ -z "$tag" ]; then
              echo "No previous tag found."
              need_build=true
            else
              base=$(git rev-parse "$tag")
              head=$(git rev-parse HEAD)
              if [ "$base" != "$head" ]; then
                echo "New commits found since $tag"
                need_build=true
              else
                echo "No new commits since $tag"
              fi
            fi
          else
            echo "Version does not contain 'dev'"
          fi
          echo "need_build=$need_build" >> "$GITHUB_OUTPUT"
      - name: Set tag name
        id: tag
        if: steps.check.outputs.need_build == 'true'
        run: |
          today=$(date -u +"%Y%m%d")
          tag_name="v${{ steps.version.outputs.version }}$today"
          echo "tag_name=$tag_name"
          echo "tag_name=$tag_name" >> "$GITHUB_OUTPUT"
      - name: Create and push tag
        if: steps.check.outputs.need_build == 'true'
        run: |
          git config user.name "github-actions"
          git config user.email "github-actions@users.noreply.github.com"
          git tag ${{ steps.tag.outputs.tag_name }}
          git push origin ${{ steps.tag.outputs.tag_name }}
      - name: Skip tagging (version is not dev or no new commits)
        if: steps.check.outputs.need_build == 'false'
        run: echo "Version is not a dev version or no new commits. Skipping tag."
  linux-wheels:
    name: Build the linux nightly wheels
    runs-on: [self-hosted, linux-build]
    needs: tag
    if: needs.tag.outputs.need_build == 'true' && github.repository == 'nunchaku-tech/nunchaku'
    strategy:
      matrix:
        python: ["3.10", "3.11", "3.12"]
        torch: ["2.5", "2.6", "2.7", "2.8", "2.9"]
    steps:
      - name: Checkout to the tag
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          ref: ${{ needs.tag.outputs.tag_name }}
          submodules: true
      - name: Show current commit
        run: git log -1 --oneline
      - name: Build wheels
        run: |
          if [[ "${{ matrix.torch }}" == "2.7" || "${{ matrix.torch }}" == "2.8" ]]; then
            cuda_version="12.8"
          else
            cuda_version="12.4"
          fi
          if [[ "${{ matrix.torch }}" == "2.9" ]]; then
            bash scripts/build_linux_wheel_torch_nightly.sh ${{ matrix.python }} ${{ matrix.torch }} 12.8
          else
            bash scripts/build_linux_wheel.sh ${{ matrix.python }} ${{ matrix.torch }} $cuda_version
          fi
      - name: Upload wheels to GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          files: dist/*.whl
          name: Nunchaku Nightly ${{ needs.tag.outputs.tag_name }}
          tag_name: ${{ needs.tag.outputs.tag_name }}
          prerelease: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Clean up
        if: always()
        run: bash scripts/linux_cleanup.sh
  windows-wheels:
    name: Build the windows nightly wheels
    runs-on: [self-hosted, windows-build]
    needs: tag
    if: needs.tag.outputs.need_build == 'true' && github.repository == 'nunchaku-tech/nunchaku'
    strategy:
      matrix:
        python: ["3.10", "3.11", "3.12"]
        torch: ["2.5", "2.6", "2.7", "2.8", "2.9"]
    steps:
      - name: Checkout to the tag
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          ref: ${{ needs.tag.outputs.tag_name }}
          submodules: true
      - name: Show current commit
        run: git log -1 --oneline
      - name: Build wheels
        shell: cmd
        run: |
          SET TORCH_VERSION=${{ matrix.torch }}
          SET PYTHON_VERSION=${{ matrix.python }}
          IF "%TORCH_VERSION%"=="2.7" (
            SET CUDA_VERSION=12.8
          ) ELSE IF "%TORCH_VERSION%"=="2.8" (
            SET CUDA_VERSION=12.8
          ) ELSE (
            SET CUDA_VERSION=12.4
          )
          call C:\Users\muyangl\miniconda3\condabin\activate.bat activate
          IF "%TORCH_VERSION%"=="2.9" (
            call scripts\build_windows_wheel_torch_nightly.cmd %PYTHON_VERSION% %TORCH_VERSION% %CUDA_VERSION%
          ) ELSE (
            call scripts\build_windows_wheel.cmd %PYTHON_VERSION% %TORCH_VERSION% %CUDA_VERSION%
          )
      - name: Upload wheels to GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          files: dist/*.whl
          name: Nunchaku Nightly ${{ needs.tag.outputs.tag_name }}
          tag_name: ${{ needs.tag.outputs.tag_name }}
          prerelease: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
