name: Publish to PyPI

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

jobs:
  build:
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            cibw_archs: "native"
          - os: windows-latest
            cibw_archs: "native"
          - os: macos-latest
            cibw_archs: "native arm64"
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - uses: pypa/cibuildwheel@v3.3.1
        env:
          CIBW_ARCHS: ${{ matrix.cibw_archs }}
          CIBW_BUILD: "cp39-* cp310-* cp311-* cp312-* cp313-*"
          CIBW_BEFORE_ALL_LINUX: "curl -sSf https://sh.rustup.rs | sh -s -- -y"
          CIBW_BEFORE_ALL_MACOS: "rustup target add x86_64-apple-darwin"
          CIBW_ENVIRONMENT_LINUX: "PATH=$HOME/.cargo/bin:$PATH"
          MACOSX_DEPLOYMENT_TARGET: "10.12"
          CIBW_SKIP: "*-musllinux* pp*"
      - uses: actions/upload-artifact@v4
        with:
          name: wheels-${{ matrix.os }}
          path: ./wheelhouse/*.whl

  publish:
    needs: build
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/')
    permissions:
      id-token: write
      contents: write
    steps:
      - uses: actions/download-artifact@v4
        with:
          pattern: wheels-*
          path: dist
          merge-multiple: true
      - uses: pypa/gh-action-pypi-publish@release/v1
