# Copyright (c) ONNX Project Contributors
#
# SPDX-License-Identifier: Apache-2.0

name: WindowsRelease

on:
  schedule:
    # Run weekly on Monday 00:00
    - cron:  '00 00 * * MON'
  push:
    branches: [main, rel-*]
  pull_request:
    branches: [main, rel-*]
  workflow_dispatch:

permissions:  # set top-level default permissions as security best practice
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name == 'workflow_dispatch' }}
  cancel-in-progress: true

jobs:
  build:
    if: github.event_name != 'pull_request' || startsWith( github.base_ref, 'rel-') || contains( github.event.pull_request.labels.*.name, 'run release CIs')
    runs-on: windows-latest
    strategy:
      matrix:
        python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
        architecture: ['x64', 'x86']
    steps:
    - name: Checkout ONNX
      uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
      with:
         path: ./onnx
         submodules: 'recursive'

    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
      with:
        python-version: ${{ matrix.python-version }}
        architecture: ${{ matrix.architecture }}

    - name: Add msbuild to PATH
      uses: microsoft/setup-msbuild@6fb02220983dee41ce7ae257b6f4d8f9bf5ed4ce # v2.0.0
      with:
        msbuild-architecture: ${{ matrix.architecture }}

    - name: Install Python dependencies
      run: |
        python -m pip install -q --upgrade pip
        cd onnx
        if ('${{ matrix.architecture }}' -eq 'x86') {
          echo "Skip installing dependencies for reference, because they don't have prebuilt wheel on x86"
          sed -i '' '/-r requirements-reference.txt/d' requirements-release.txt
        }
        python -m pip install -q -r requirements-release.txt
        python -m pip install cmake

    - name: Build ONNX wheel
      run: |
        $arch = 'x64'
        if ('${{ matrix.architecture }}' -eq 'x86') {
          $arch = 'Win32'
        }

        . .\onnx\workflow_scripts\protobuf\build_protobuf_win.ps1 -arch $arch
        cd onnx

        echo "Install ONNX"
        $Env:ONNX_ML=1
        $Env:CMAKE_ARGS="-DONNX_USE_PROTOBUF_SHARED_LIBS=OFF -DONNX_USE_LITE_PROTO=ON"

        if ('${{ github.event_name }}' -eq 'schedule') {
          echo "Build weekly PyPI package"
          (Get-Content -Path 'pyproject.toml') | ForEach-Object { $_ -replace 'name = "onnx"', 'name = "onnx-weekly"' } | Set-Content -Path 'pyproject.toml'
          $Env:ONNX_PREVIEW_BUILD=1
        }
        python -m build --wheel
        Get-ChildItem -Path dist/*.whl | foreach {python -m pip install --upgrade $_.fullname}

    - name: Test the installed wheel
      run: |
        cd onnx
        pytest

    - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3.1.3
      with:
        name: wheels
        path: ./onnx/dist

    - name: Upload onnx-weekly wheel to PyPI/PyPI weekly
      if: (github.event_name == 'schedule') # Only triggered by weekly event
      run: |
        twine upload --verbose onnx/dist/*.whl --repository-url https://upload.pypi.org/legacy/ -u ${{ secrets.ONNXWEEKLY_USERNAME }} -p ${{ secrets.ONNXWEEKLY_TOKEN }}

    - name: Verify ONNX with the latest numpy
      if: ${{ always() }}
      run: |
        cd onnx
        python -m pip uninstall -y numpy onnx
        python -m pip install numpy
        Get-ChildItem -Path dist/*.whl | foreach {python -m pip install --upgrade $_.fullname}
        pytest

    - name: Verify ONNX with the latest protobuf
      if: ${{ always() }}
      run: |
        cd onnx
        python -m pip uninstall -y protobuf onnx
        # https://github.com/protocolbuffers/protobuf/issues/18045
        # The protobuf Python package version 5.28.0 is broken on Windows.
        # Use the version prior to it until it gets fixed or a newer version is available.
        python -m pip install protobuf!=5.28.0
        Get-ChildItem -Path dist/*.whl | foreach {python -m pip install --upgrade $_.fullname}
        pytest

    - name: Verify ONNX with the minimumly supported packages
      if: ${{ always() }}
      run: |
        cd onnx
        python -m pip uninstall -y protobuf numpy onnx
        python -m pip install -r requirements-min.txt
        Get-ChildItem -Path dist/*.whl | foreach {python -m pip install --upgrade $_.fullname}
        pytest

    - name: Verify ONNX with ONNX Runtime PyPI package
      if: matrix.python-version != '3.12'
      run: |
        cd onnx
        python -m pip uninstall -y protobuf numpy
        python -m pip install -q -r requirements-release.txt
        python -m pip install -q onnxruntime==1.17.3
        $Env:ORT_MAX_IR_SUPPORTED_VERSION=9
        $Env:ORT_MAX_ML_OPSET_SUPPORTED_VERSION=3
        $Env:ORT_MAX_ONNX_OPSET_SUPPORTED_VERSION=20
        pytest
