
name: "Release Stable Version"

on:
  workflow_call:
    inputs:
      git_tag:
        description: 'Git tag'
        required: true
        type: string
      cache_tag:
        description: 'Cached dependencies tag'
        required: true
        type: string
        default: "cu129"
      python_minor:
        description: 'Python minor version'
        required: true
        type: string
        default: "13"
      python_patch:
        description: 'Python patch version'
        required: true
        type: string
        default: "6"
      rel_name:
        description: 'Release name'
        required: true
        type: string
        default: "nvidia"
      rel_extra_name:
        description: 'Release extra name'
        required: false
        type: string
        default: ""
      test_release:
        description: 'Test Release'
        required: true
        type: boolean
        default: true
  workflow_dispatch:
    inputs:
      git_tag:
        description: 'Git tag'
        required: true
        type: string
      cache_tag:
        description: 'Cached dependencies tag'
        required: true
        type: string
        default: "cu129"
      python_minor:
        description: 'Python minor version'
        required: true
        type: string
        default: "13"
      python_patch:
        description: 'Python patch version'
        required: true
        type: string
        default: "6"
      rel_name:
        description: 'Release name'
        required: true
        type: string
        default: "nvidia"
      rel_extra_name:
        description: 'Release extra name'
        required: false
        type: string
        default: ""
      test_release:
        description: 'Test Release'
        required: true
        type: boolean
        default: true

jobs:
  package_comfy_windows:
    permissions:
      contents: "write"
      packages: "write"
      pull-requests: "read"
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ inputs.git_tag }}
          fetch-depth: 150
          persist-credentials: false
      - uses: actions/cache/restore@v4
        id: cache
        with:
          path: |
            ${{ inputs.cache_tag }}_python_deps.tar
            update_comfyui_and_python_dependencies.bat
          key: ${{ runner.os }}-build-${{ inputs.cache_tag }}-${{ inputs.python_minor }}
      - shell: bash
        run: |
          mv ${{ inputs.cache_tag }}_python_deps.tar ../
          mv update_comfyui_and_python_dependencies.bat ../
          cd ..
          tar xf ${{ inputs.cache_tag }}_python_deps.tar
          pwd
          ls

      - shell: bash
        run: |
          cd ..
          cp -r ComfyUI ComfyUI_copy
          curl https://www.python.org/ftp/python/3.${{ inputs.python_minor }}.${{ inputs.python_patch }}/python-3.${{ inputs.python_minor }}.${{ inputs.python_patch }}-embed-amd64.zip -o python_embeded.zip
          unzip python_embeded.zip -d python_embeded
          cd python_embeded
          echo ${{ env.MINOR_VERSION }}
          echo 'import site' >> ./python3${{ inputs.python_minor }}._pth
          curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
          ./python.exe get-pip.py
          ./python.exe -s -m pip install ../${{ inputs.cache_tag }}_python_deps/*

          grep comfy ../ComfyUI/requirements.txt > ./requirements_comfyui.txt
          ./python.exe -s -m pip install -r requirements_comfyui.txt
          rm requirements_comfyui.txt

          sed -i '1i../ComfyUI' ./python3${{ inputs.python_minor }}._pth

          if test -f ./Lib/site-packages/torch/lib/dnnl.lib; then
            rm ./Lib/site-packages/torch/lib/dnnl.lib #I don't think this is actually used and I need the space
            rm ./Lib/site-packages/torch/lib/libprotoc.lib
            rm ./Lib/site-packages/torch/lib/libprotobuf.lib
          fi

          cd ..

          git clone --depth 1 https://github.com/comfyanonymous/taesd
          cp taesd/*.safetensors ./ComfyUI_copy/models/vae_approx/

          mkdir ComfyUI_windows_portable
          mv python_embeded ComfyUI_windows_portable
          mv ComfyUI_copy ComfyUI_windows_portable/ComfyUI

          cd ComfyUI_windows_portable

          mkdir update
          cp -r ComfyUI/.ci/update_windows/* ./update/
          cp -r ComfyUI/.ci/windows_${{ inputs.rel_name }}_base_files/* ./
          cp ../update_comfyui_and_python_dependencies.bat ./update/

          echo 'local-portable' > ComfyUI/.comfy_environment

          cd ..

          "C:\Program Files\7-Zip\7z.exe" a -t7z -m0=lzma2 -mx=9 -mfb=128 -md=768m -ms=on -mf=BCJ2 ComfyUI_windows_portable.7z ComfyUI_windows_portable
          mv ComfyUI_windows_portable.7z ComfyUI/ComfyUI_windows_portable_${{ inputs.rel_name }}${{ inputs.rel_extra_name }}.7z

      - shell: bash
        if: ${{ inputs.test_release }}
        run: |
          cd ..
          cd ComfyUI_windows_portable
          python_embeded/python.exe -s ComfyUI/main.py --quick-test-for-ci --cpu

          python_embeded/python.exe -s ./update/update.py ComfyUI/

          ls

      - name: Upload binaries to release
        uses: softprops/action-gh-release@v2
        with:
          files: ComfyUI_windows_portable_${{ inputs.rel_name }}${{ inputs.rel_extra_name }}.7z
          tag_name: ${{ inputs.git_tag }}
          draft: true
          overwrite_files: true
