Skip to content

Fix build python wheels #97

Fix build python wheels

Fix build python wheels #97

# Build wheels for many platforms
# see: https://github.com/pypa/cibuildwheel
name: Build Python wheels
on:
push:
branches:
- main
paths:
- 'nlpo3-python/**'
- '!nlpo3-python/**.md'
- '!nlpo3-python/notebooks/**'
- '.github/workflows/build-python-wheels.yml'
pull_request:
branches:
- main
paths:
- 'nlpo3-python/**'
- '!nlpo3-python/**.md'
- '!nlpo3-python/notebooks/**'
- '.github/workflows/build-python-wheels.yml'
release:
types: [published]
# Manual run
workflow_dispatch: {}
jobs:
echo_github_env:
name: Echo GitHub environment variables
runs-on: ubuntu-latest
steps:
- run: |
echo "github.event.action : ${{ github.event.action }}"
echo "github.event_name : ${{ github.event_name }}"
echo "github.ref : ${{ github.ref }}"
echo "github.event.ref : ${{ github.event.ref }}"
echo "github.event.ref_type : ${{ github.event.ref_type }}"
# Check whether to build the wheels and the source tarball
check_build_trigger:
name: Check build trigger
runs-on: ubuntu-latest
# Not for forks
if: github.repository == 'pythainlp/nlpo3'
outputs:
build: ${{ steps.check_build_trigger.outputs.build }}
steps:
- name: Checkout source code
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
- id: check_build_trigger
name: Check build trigger
run: bash build_tools/github/check_build_trigger.sh
build_wheels:
name: Build Python wheel for ${{ matrix.python }}-${{ matrix.platform_id }}-${{ matrix.manylinux_image }}
runs-on: ${{ matrix.os }}
needs: check_build_trigger
if: needs.check_build_trigger.outputs.build
strategy:
# Ensure that a wheel builder finishes even if another fails
fail-fast: false
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
python: [cp39, cp310, cp311, cp312, cp313]
bitness: [32, 64]
manylinux_image: [manylinux2014]
include:
- os: macos-latest
bitness: 64
platform_id: macosx_x86_64
- os: ubuntu-latest
bitness: 64
platform_id: manylinux_x86_64
- os: ubuntu-latest
bitness: 32
platform_id: manylinux_i686
- os: windows-latest
bitness: 64
platform_id: win_amd64
- os: windows-latest
bitness: 32
platform_id: win32
exclude:
- os: macos-latest
bitness: 32
steps:
- name: Checkout source code
uses: actions/checkout@v4
- name: Setup Rust toolchain
uses: actions-rust-lang/setup-rust-toolchain@v1
- name: Setup Python
uses: actions/setup-python@v5
with:
cache: "pip"
- name: Update pip
run: |
python -m pip install --upgrade pip
- name: Build Python wheels
uses: pypa/cibuildwheel@v2.21.3
with:
package-dir: nlpo3-python
output-dir: wheelhouse
env:
CIBW_BUILD_VERBOSITY: 1
# See build selector name at:
# https://cibuildwheel.readthedocs.io/en/stable/options/#build-skip
CIBW_BUILD: ${{ matrix.python }}-${{ matrix.platform_id }}
CIBW_ENVIRONMENT_LINUX: PATH="$HOME/.cargo/bin:$PATH"
CIBW_ENVIRONMENT_MACOS: MACOSX_DEPLOYMENT_TARGET=10.13
PATH="$HOME/.cargo/bin:$PATH"
CC=/usr/bin/clang
CXX=/usr/bin/clang++
CIBW_MANYLINUX_I686_IMAGE: ${{ matrix.manylinux_image }}
CIBW_MANYLINUX_X86_64_IMAGE: ${{ matrix.manylinux_image }}
CIBW_MANYLINUX_PYPY_I686_IMAGE: ${{ matrix.manylinux_image }}
CIBW_MANYLINUX_PYPY_X86_64_IMAGE: ${{ matrix.manylinux_image }}
CIBW_BEFORE_BUILD_LINUX: >
curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain=stable --profile=minimal -y
CIBW_BEFORE_BUILD_WINDOWS: 'rustup default stable'
- name: Store artifacts
uses: actions/upload-artifact@v4
with:
path: ./wheelhouse/*.whl
build_sdist:
name: Build Python source distribution
runs-on: ubuntu-latest
needs: check_build_trigger
if: needs.check_build_trigger.outputs.build
steps:
- name: Checkout source code
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
cache: "pip"
- name: Build source distribution
run: |
cd nlpo3-python
bash ../build_tools/github/build_source.sh
- name: Store artifacts
uses: actions/upload-artifact@v4
with:
path: nlpo3-python/dist/*.tar.gz
# May consider remove this job if no longer needed
# GitHub runners now have arm64 architecture
build_wheels_macos_on_self_hosted:
name: Build wheels on M1
runs-on: self-hosted
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
steps:
- name: Checkout source code
uses: actions/checkout@v4
- name: Test
run: cargo test
- name: Build wheels
run: |
source ~/miniconda3/etc/profile.d/conda.sh
conda init zsh
cd nlpo3-python
conda create --name python${{ matrix.python-version }} python=${{ matrix.python-version }} -y
conda activate python${{ matrix.python-version }}
python -V
python -m pip install maturin
python -m pip list
python -m pip wheel . -w build
conda deactivate
cd ..
shell: zsh {0}
- name: Store artifacts
uses: actions/upload-artifact@v4
with:
path: ./nlpo3-python/build/*.whl
publish_pypi:
name: Publish Python package to PyPI
runs-on: ubuntu-latest
needs: [build_wheels, build_sdist, build_wheels_macos_on_self_hosted]
# upload to PyPI on every tag starting with 'v'
#if: github.event_name == 'push' && startsWith(github.event.ref, 'v')
# alternatively, to publish when a GitHub Release is created, use the following rule:
if: github.event_name == 'release' && github.event.action == 'published'
steps:
- name: Retrieve artifacts
uses: actions/download-artifact@v4
with:
name: artifact
path: dist
- name: Publish package
uses: pypa/gh-action-pypi-publish@v1.12.2
with:
skip-existing: true
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}