name: Qdrant Edge Release Build

on:
  workflow_dispatch:
    inputs:
      publish:
        description: 'Publish to PyPI'
        type: boolean
        default: false
        required: true


jobs:
  edge-py-linux:
    name: Build Python wheels [Linux]

    runs-on: ubuntu-latest

    strategy:
      matrix:
        target: [x86_64, aarch64]
        manylinux: [manylinux_2_17, manylinux_2_28]
        python:
          - python3.10
          - pypy3.11

        include:
          - arch: x86_64
            target: x86_64-unknown-linux-musl
            manylinux: musllinux_1_2
            python: python3.10

          - arch: aarch64
            target: aarch64-unknown-linux-musl
            manylinux: musllinux_1_2
            python: python3.10

        exclude:
          - target: aarch64
            manylinux: manylinux_2_17

    steps:
      - name: Checkout Qdrant
        uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3

      - name: Build Python wheel
        uses: PyO3/maturin-action@e83996d129638aa358a18fbd1dfb82f0b0fb5d3b # v1.51.0
        with:
          command: build
          args: --release --features abi3 --interpreter ${{ matrix.python }} --out dist
          working-directory: lib/edge/python
          target: ${{ matrix.target }}
          manylinux: ${{ matrix.manylinux }}

      - name: Upload Python wheel
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: qdrant-edge-${{ matrix.manylinux }}-${{ matrix.arch || matrix.target }}-${{ matrix.python }}
          path: lib/edge/python/dist


  edge-py-macos:
    name: Build Python wheels [macOS]

    runs-on: macos-latest

    strategy:
      matrix:
        target: [x86_64, aarch64]
        python:
          - label: python3.10
            version: '3.10'
          - pypy3.11

    steps:
      - name: Install Rust
        uses: dtolnay/rust-toolchain@631a55b12751854ce901bb631d5902ceb48146f7 # stable

      - name: Install Python
        id: setup-python
        uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
        with:
          python-version: ${{ matrix.python.version || matrix.python }}

      - name: Checkout Qdrant
        uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3

      - name: Build Python wheel
        uses: PyO3/maturin-action@e83996d129638aa358a18fbd1dfb82f0b0fb5d3b # v1.51.0
        with:
          command: build
          args: --release --features abi3 --interpreter ${{ steps.setup-python.outputs.python-path }} --out dist
          working-directory: lib/edge/python
          target: ${{ matrix.target }}

      - name: Upload Python wheel
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: qdrant-edge-macos-${{ matrix.target }}-${{ matrix.python.label || matrix.python }}
          path: lib/edge/python/dist


  edge-py-windows:
    name: Build Python wheels [Windows]

    runs-on: windows-latest

    strategy:
      matrix:
        target: [x86_64]
        python:
          - label: python3.10
            version: '3.10'
          - pypy3.11

    steps:
      - name: Install Rust
        uses: dtolnay/rust-toolchain@631a55b12751854ce901bb631d5902ceb48146f7 # stable

      - name: Install Python
        id: setup-python
        uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
        with:
          python-version: ${{ matrix.python.version || matrix.python }}

      - name: Checkout Qdrant
        uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3

      - name: Build Python wheel
        uses: PyO3/maturin-action@e83996d129638aa358a18fbd1dfb82f0b0fb5d3b # v1.51.0
        with:
          command: build
          args: --release --features abi3 --interpreter ${{ steps.setup-python.outputs.python-path }} --out dist
          working-directory: lib/edge/python
          target: ${{ matrix.target }}

      - name: Upload Python wheel
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: qdrant-edge-windows-${{ matrix.target }}-${{ matrix.python.label || matrix.python }}
          path: lib/edge/python/dist


  edge-py-test:
    name: Test Python wheels [${{ matrix.os }}, ${{ matrix.python }}]

    runs-on: ${{ matrix.os }}
    needs:
      - edge-py-linux
      - edge-py-macos
      - edge-py-windows

    strategy:
      fail-fast: false
      matrix:
        os:
          - macos-latest
          - ubuntu-24.04-arm
          - ubuntu-latest
          - windows-latest
        python:
          - '3.10'
          - 'pypy3.11'

    steps:
      - name: Checkout Qdrant
        uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3

      - name: Install Just
        uses: ./.github/actions/setup-just

      - name: Install uv
        uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b # v8.1.0

      - name: Install Python
        uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
        with:
          python-version: ${{ matrix.python }}

      - name: Download wheels
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          pattern: qdrant-edge-*
          merge-multiple: true
          path: lib/edge/python/dist

      - name: Setup Qdrant
        uses: ./.github/actions/setup-qdrant

      - name: Install and test wheel
        working-directory: lib/edge/python
        run: |
          uv venv --allow-existing --python ${{ matrix.python }}
          uv pip install --no-index --find-links dist/ qdrant-edge-py
          uv pip install requests
          just py-examples


  merge-artifacts:
    name: Merge release artifacts

    runs-on: ubuntu-latest
    needs:
      - edge-py-linux
      - edge-py-macos
      - edge-py-windows
      - edge-py-test

    if: inputs.publish

    steps:
      - name: Merge release artifacts
        uses: actions/upload-artifact/merge@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: qdrant-edge
          pattern: qdrant-edge-*
          delete-merged: false


  publish:
    name: Publish to PyPI

    runs-on: ubuntu-latest
    needs:
      - merge-artifacts

    if: inputs.publish

    steps:
      - name: Install Python
        uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0

      - name: Download release wheels
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          name: qdrant-edge

      - name: Publish to PyPI
        env:
          TWINE_USERNAME: __token__
          TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN_EDGE }}
        run: |
          pip install --upgrade twine
          twine upload --skip-existing *
