name: Conda build
description: Builds FAISS inside a Conda environment and uploads to repository when label is provided.
inputs:
  label:
    description: "The label to be used for uploads to Conda."
    default: ""
    required: false
  cuda:
    description: "CUDA toolkit version to use."
    default: ""
    required: false
  raft:
    description: "Enable RAFT support."
    default: ""
    required: false
runs:
  using: composite
  steps:
    - name: Choose shell
      shell: bash
      id: choose_shell
      run: |
        # Use pwsh on Windows; bash everywhere else
        if [ "${{ runner.os }}" != "Windows" ]; then
          echo "shell=bash" >> "$GITHUB_OUTPUT"
        else
          echo "shell=pwsh" >> "$GITHUB_OUTPUT"
        fi
    - name: Setup miniconda
      uses: conda-incubator/setup-miniconda@v3
      with:
        python-version: '3.11'
        miniconda-version:  latest
    - name: Install conda build tools
      shell: ${{ steps.choose_shell.outputs.shell }}
      run: |
        conda update -y -q conda
        conda install -y -q conda-build
    - name: Enable anaconda uploads
      if: inputs.label != ''
      shell: ${{ steps.choose_shell.outputs.shell }}
      env:
        PACKAGE_TYPE: ${{ inputs.label }}
      run: |
        conda install -y -q anaconda-client
        conda config --set anaconda_upload yes
    - name: Conda build (CPU)
      if: inputs.label == '' && inputs.cuda == ''
      shell: ${{ steps.choose_shell.outputs.shell }}
      working-directory: conda
      run: |
        conda build faiss --python 3.11 -c pytorch
    - name: Conda build (CPU) w/ anaconda upload
      if: inputs.label != '' && inputs.cuda == ''
      shell: ${{ steps.choose_shell.outputs.shell }}
      working-directory: conda
      env:
        PACKAGE_TYPE: ${{ inputs.label }}
      run: |
        conda build faiss --user pytorch --label ${{ inputs.label }} -c pytorch
    - name: Conda build (GPU)
      if: inputs.label == '' && inputs.cuda != '' && inputs.raft == ''
      shell: ${{ steps.choose_shell.outputs.shell }}
      working-directory: conda
      run: |
        conda build faiss-gpu --variants '{ "cudatoolkit": "${{ inputs.cuda }}" }' \
            -c pytorch -c nvidia/label/cuda-${{ inputs.cuda }} -c nvidia
    - name: Conda build (GPU) w/ anaconda upload
      if: inputs.label != '' && inputs.cuda != '' && inputs.raft == ''
      shell: ${{ steps.choose_shell.outputs.shell }}
      working-directory: conda
      env:
        PACKAGE_TYPE: ${{ inputs.label }}
      run: |
        conda build faiss-gpu --variants '{ "cudatoolkit": "${{ inputs.cuda }}" }' \
            --user pytorch --label ${{ inputs.label }} -c pytorch -c nvidia/label/cuda-${{ inputs.cuda }} -c nvidia
    - name: Conda build (GPU w/ RAFT)
      if: inputs.label == '' && inputs.cuda != '' && inputs.raft != ''
      shell: ${{ steps.choose_shell.outputs.shell }}
      working-directory: conda
      run: |
        conda build faiss-gpu-raft --variants '{ "cudatoolkit": "${{ inputs.cuda }}" }' \
            -c pytorch -c nvidia/label/cuda-${{ inputs.cuda }} -c nvidia -c rapidsai -c rapidsai-nightly -c conda-forge
    - name: Conda build (GPU w/ RAFT) w/ anaconda upload
      if: inputs.label != '' && inputs.cuda != '' && inputs.raft != ''
      shell: ${{ steps.choose_shell.outputs.shell }}
      working-directory: conda
      env:
        PACKAGE_TYPE: ${{ inputs.label }}
      run: |
        conda build faiss-gpu-raft --variants '{ "cudatoolkit": "${{ inputs.cuda }}" }' \
            --user pytorch --label ${{ inputs.label }} -c pytorch -c nvidia/label/cuda-${{ inputs.cuda }} -c nvidia -c rapidsai -c rapidsai-nightly -c conda-forge
