|
| 1 | +name: Python wheels |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + tags: ["py-v*", "py-test-*"] |
| 7 | + pull_request: |
| 8 | + branches: [main] |
| 9 | + workflow_dispatch: # manual ad-hoc builds from any branch |
| 10 | + |
| 11 | +concurrency: |
| 12 | + # Tag pushes get their own group so publishes never get cancelled. |
| 13 | + group: >- |
| 14 | + ${{ github.workflow }}-${{ github.ref }}-${{ startsWith(github.ref, 'refs/tags/') && 'publish' || 'branch' }} |
| 15 | + cancel-in-progress: ${{ !startsWith(github.ref, 'refs/tags/') }} |
| 16 | + |
| 17 | +env: |
| 18 | + CARGO_TERM_COLOR: always |
| 19 | + |
| 20 | +jobs: |
| 21 | + build: |
| 22 | + name: Build wheel (${{ matrix.target.label }}) |
| 23 | + runs-on: ${{ matrix.target.runner }} |
| 24 | + strategy: |
| 25 | + fail-fast: false |
| 26 | + matrix: |
| 27 | + target: |
| 28 | + # `manylinux: "2_28"` makes maturin-action run the build inside the |
| 29 | + # official PyPA manylinux_2_28 container (Rocky Linux 8 / glibc 2.28). |
| 30 | + # Without this, the build runs on the host (Ubuntu glibc 2.39) and |
| 31 | + # produces a wheel that fails the auditwheel manylinux_2_28 check. |
| 32 | + - label: linux-x86_64 |
| 33 | + runner: ubuntu-latest |
| 34 | + target: x86_64-unknown-linux-gnu |
| 35 | + manylinux: "2_28" |
| 36 | + - label: macos-universal2 |
| 37 | + runner: macos-latest |
| 38 | + target: universal2-apple-darwin |
| 39 | + manylinux: "auto" |
| 40 | + - label: windows-x86_64 |
| 41 | + runner: windows-latest |
| 42 | + target: x86_64-pc-windows-msvc |
| 43 | + manylinux: "auto" |
| 44 | + steps: |
| 45 | + - uses: actions/checkout@v4 |
| 46 | + |
| 47 | + - uses: actions/setup-python@v5 |
| 48 | + with: |
| 49 | + python-version: "3.10" # abi3 — any 3.10+ works for building |
| 50 | + |
| 51 | + - uses: dtolnay/rust-toolchain@stable |
| 52 | + with: |
| 53 | + targets: ${{ matrix.target.label == 'macos-universal2' && 'x86_64-apple-darwin,aarch64-apple-darwin' || '' }} |
| 54 | + |
| 55 | + - uses: Swatinem/rust-cache@v2 |
| 56 | + with: |
| 57 | + workspaces: bonsai-py |
| 58 | + key: ${{ matrix.target.label }} |
| 59 | + |
| 60 | + - name: Tag/version guard (tag pushes only) |
| 61 | + if: startsWith(github.ref, 'refs/tags/') |
| 62 | + shell: bash |
| 63 | + run: | |
| 64 | + tag="${GITHUB_REF#refs/tags/}" |
| 65 | + version="${tag#py-v}" |
| 66 | + version="${version#py-test-}" |
| 67 | + cargo_version=$(grep -m1 '^version' bonsai-py/Cargo.toml | sed -E 's/.*"([^"]+)".*/\1/') |
| 68 | + if [ "$version" != "$cargo_version" ]; then |
| 69 | + echo "::error::Tag version '$version' does not match Cargo.toml version '$cargo_version'." |
| 70 | + exit 1 |
| 71 | + fi |
| 72 | +
|
| 73 | + - uses: PyO3/maturin-action@v1 |
| 74 | + with: |
| 75 | + working-directory: bonsai-py |
| 76 | + command: build |
| 77 | + target: ${{ matrix.target.target }} |
| 78 | + manylinux: ${{ matrix.target.manylinux }} |
| 79 | + args: --release --out dist --strip |
| 80 | + |
| 81 | + - name: Verify wheel (Linux/macOS only — Windows venv quirks) |
| 82 | + if: matrix.target.runner != 'windows-latest' |
| 83 | + shell: bash |
| 84 | + run: | |
| 85 | + python -m venv .venv-test |
| 86 | + source .venv-test/bin/activate |
| 87 | + pip install --upgrade pip |
| 88 | + pip install pytest pytest-timeout mypy |
| 89 | + pip install bonsai-py/dist/*.whl |
| 90 | + python -c "import bonsai_bt; print(bonsai_bt.__version__)" |
| 91 | + pytest bonsai-py/tests/ |
| 92 | +
|
| 93 | + - name: Wheel size sanity (Linux/macOS only) |
| 94 | + if: matrix.target.runner != 'windows-latest' |
| 95 | + shell: bash |
| 96 | + run: | |
| 97 | + size=$(stat -c%s bonsai-py/dist/*.whl 2>/dev/null || stat -f%z bonsai-py/dist/*.whl) |
| 98 | + ceiling=$((5 * 1024 * 1024)) |
| 99 | + if [ "$size" -gt "$ceiling" ]; then |
| 100 | + echo "::error::Wheel size $size exceeds 5MB ceiling." |
| 101 | + exit 1 |
| 102 | + fi |
| 103 | + echo "Wheel size: $size bytes (under 5MB ceiling)." |
| 104 | +
|
| 105 | + - uses: actions/upload-artifact@v4 |
| 106 | + with: |
| 107 | + name: wheel-${{ matrix.target.label }} |
| 108 | + path: bonsai-py/dist/*.whl |
| 109 | + retention-days: ${{ startsWith(github.ref, 'refs/tags/') && 90 || 14 }} |
| 110 | + |
| 111 | + sdist: |
| 112 | + name: Build sdist |
| 113 | + runs-on: ubuntu-latest |
| 114 | + steps: |
| 115 | + - uses: actions/checkout@v4 |
| 116 | + - uses: actions/setup-python@v5 |
| 117 | + with: |
| 118 | + python-version: "3.10" |
| 119 | + - uses: PyO3/maturin-action@v1 |
| 120 | + with: |
| 121 | + working-directory: bonsai-py |
| 122 | + command: sdist |
| 123 | + args: --out dist |
| 124 | + - uses: actions/upload-artifact@v4 |
| 125 | + with: |
| 126 | + name: sdist |
| 127 | + path: bonsai-py/dist/*.tar.gz |
| 128 | + retention-days: ${{ startsWith(github.ref, 'refs/tags/') && 90 || 14 }} |
| 129 | + |
| 130 | + verify-sdist: |
| 131 | + name: Verify sdist builds from source |
| 132 | + needs: sdist |
| 133 | + runs-on: ubuntu-latest |
| 134 | + steps: |
| 135 | + - uses: actions/setup-python@v5 |
| 136 | + with: |
| 137 | + python-version: "3.10" |
| 138 | + - uses: dtolnay/rust-toolchain@stable |
| 139 | + - uses: actions/download-artifact@v4 |
| 140 | + with: |
| 141 | + name: sdist |
| 142 | + path: dist |
| 143 | + - name: Install + smoke-test from sdist |
| 144 | + run: | |
| 145 | + python -m venv .venv-sdist |
| 146 | + source .venv-sdist/bin/activate |
| 147 | + pip install --upgrade pip |
| 148 | + pip install --no-binary :all: dist/*.tar.gz |
| 149 | + python -c "import bonsai_bt; print(bonsai_bt.__version__)" |
| 150 | +
|
| 151 | + publish: |
| 152 | + name: Publish to PyPI / TestPyPI |
| 153 | + needs: [build, sdist, verify-sdist] |
| 154 | + if: startsWith(github.ref, 'refs/tags/py-v') || startsWith(github.ref, 'refs/tags/py-test-') |
| 155 | + runs-on: ubuntu-latest |
| 156 | + environment: |
| 157 | + name: ${{ startsWith(github.ref, 'refs/tags/py-test-') && 'testpypi' || 'pypi' }} |
| 158 | + permissions: |
| 159 | + id-token: write # OIDC for Trusted Publishing |
| 160 | + steps: |
| 161 | + - uses: actions/download-artifact@v4 |
| 162 | + with: |
| 163 | + path: dist |
| 164 | + pattern: wheel-* |
| 165 | + merge-multiple: true |
| 166 | + |
| 167 | + - uses: actions/download-artifact@v4 |
| 168 | + with: |
| 169 | + name: sdist |
| 170 | + path: dist |
| 171 | + |
| 172 | + - name: List artifacts to publish |
| 173 | + run: ls -la dist/ |
| 174 | + |
| 175 | + - uses: pypa/gh-action-pypi-publish@release/v1 |
| 176 | + with: |
| 177 | + repository-url: >- |
| 178 | + ${{ startsWith(github.ref, 'refs/tags/py-test-') && 'https://test.pypi.org/legacy/' || 'https://upload.pypi.org/legacy/' }} |
| 179 | + packages-dir: dist |
| 180 | + skip-existing: true |
0 commit comments