# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Deploy new version on pypi.org

on:
  release:
    types:
      - published
  workflow_dispatch:

jobs:
  build_wheels:
    name: Build wheels on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        os: [ubuntu-latest, macos-13, windows-latest]
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0  # to fetch all branches and *tags* (needed to get version number correctly)
  
      - uses: actions/setup-python@v5
        name: Install Python
        with:
          python-version: '3.10'
          
      - name: Build wheels
        uses: pypa/cibuildwheel@v3.0.0
        env:
          CIBW_BUILD: cp39-* cp310-* cp311-* cp312-*
          CIBW_BUILD_VERBOSITY: 1
          CIBW_BEFORE_ALL_LINUX: ./.github/ci-scripts/before_install.sh
          CIBW_BEFORE_ALL_MACOS: ./.github/ci-scripts/before_install_macos.sh

      - uses: actions/upload-artifact@v4
        with:
          name: cibw-wheels-${{ matrix.os }}-${{ strategy.job-index }}
          path: ./wheelhouse/*.whl

  build_sdist:
    name: Build source distribution
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0  # to fetch all branches and *tags* (needed to get version number correctly)

      - uses: actions/setup-python@v5
        name: Install Python
        with:
          python-version: '3.10'

      - name: Build sdist
        run: |
          pip install build
          python -m build --sdist

      - uses: actions/upload-artifact@v4
        with:
          name: cibw-sdist
          path: dist/*.tar.gz

  upload_pypi:
    needs: [build_wheels, build_sdist]
    runs-on: ubuntu-latest
    #if: github.event_name == 'release' && github.event.action == 'published'
    permissions:
      # IMPORTANT: this permission is mandatory for trusted publishing
      id-token: write
    steps:
      - uses: actions/download-artifact@v4
        with:
          pattern: cibw-*
          path: dist
          merge-multiple: true

      - name: Publish package on PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        # With the use of OIDC, API tokens are no longer needed
        # See https://docs.pypi.org/trusted-publishers/using-a-publisher/ for more info