
name: "Build and Publish Python Packages"

on:
  push:
    tags: "v[0-9]+.[0-9]+.[0-9]+"

jobs:

  build_sdist:

    name: "Source distribution"
    runs-on: ubuntu-latest

    steps:

      - name: "Checkout the repository"
        uses: actions/checkout@v4
        with:
          submodules: true

      - name: "Set up Python"
        uses: actions/setup-python@v4
        with:
          python-version: "3.12"

      - name: "Install python dependencies"
        run: |
          pip install setuptools

      - name: "Build source distribution"
        run: |
          python setup.py sdist

      - name: "Upload artifacts"
        uses: actions/upload-artifact@v3
        with:
          name: sdist
          path: dist/
          retention-days: 1

  build_wheels:

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

    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-22.04
          - windows-2019
          - macos-11

    steps:

      - name: "Checkout the repository"
        uses: actions/checkout@v4
        with:
          submodules: true

      - name: "Build wheels"
        uses: pypa/cibuildwheel@v2.16.1
        env:
          CIBW_ARCHS_MACOS: x86_64 arm64 universal2
          CIBW_SKIP: cp*-win32

      - name: "Upload artifacts"
        uses: actions/upload-artifact@v3
        with:
          name: wheels
          path: ./wheelhouse/*.whl
          retention-days: 1

  publish_pypi:

    name: "Publish packages on PyPI"
    runs-on: ubuntu-latest
    needs:
      - build_sdist
      - build_wheels

    steps:

      - name: "Download artifacts"
        uses: actions/download-artifact@v3

      - name: "Move packages to the dist/ folder"
        run: |
          mkdir dist/
          mv sdist/* dist/
          mv wheels/*.whl dist/

      - name: "Publish packages on PyPI"
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          password: ${{ secrets.PYPI_API_TOKEN }}
