Release Python Package to PyPI #141
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Release Python Package to PyPI | |
| on: | |
| workflow_dispatch: | |
| release: | |
| types: [published] | |
| jobs: | |
| # ────────────────────────────────────── | |
| # 1. Build PMMA_Core for each OS once | |
| # ────────────────────────────────────── | |
| build-core-linux: | |
| name: Build Linux wheels (all versions) | |
| runs-on: ubuntu-latest | |
| env: | |
| PYTHON_TAGS: "cp38-cp38 cp39-cp39 cp310-cp310 cp311-cp311" | |
| BUILD_DIR: pmma/build | |
| LIB_DIR: pmma/lib | |
| TEMP_DIR: temporary | |
| CMAKE_TEMP_DIR: temporary/cmake | |
| steps: | |
| - name: Checkout repo | |
| uses: actions/checkout@v2 | |
| with: | |
| fetch-depth: 0 | |
| - name: Ensure dist directory exists | |
| run: mkdir -p dist | |
| - name: Build all Linux wheels in parallel inside Docker | |
| if: runner.os == 'Linux' | |
| run: | | |
| docker run --rm \ | |
| -v "${{ github.workspace }}:/PMMA" \ | |
| -v "${{ github.workspace }}/dist:/output_dist" \ | |
| -e PYTHON_TAGS="cp38-cp38 cp39-cp39 cp310-cp310 cp311-cp311" \ | |
| quay.io/pypa/manylinux_2_28_x86_64 \ | |
| /bin/bash -c ' | |
| set -e | |
| yum install -y cmake make gcc gcc-c++ mesa-libGL-devel libXrandr-devel libXinerama-devel libXcursor-devel libXi-devel wayland-devel libxkbcommon-devel ninja-build | |
| build_for_python() { | |
| tag=$1 | |
| dir="/tmp/build_$tag" | |
| cp -r /PMMA "$dir" | |
| cd "$dir" | |
| PYTHON_BIN="/opt/python/$tag/bin/python" | |
| PYTHON_MAJOR_MINOR=$($PYTHON_BIN -c "import sys; print(f\"{sys.version_info.major}.{sys.version_info.minor}\")") | |
| PYTHON_INCLUDE="/opt/python/$tag/include/python${PYTHON_MAJOR_MINOR}m" | |
| PYTHON_LIBRARY="/opt/python/$tag/lib/libpython${PYTHON_MAJOR_MINOR}.so" | |
| $PYTHON_BIN -m pip install --upgrade pip setuptools wheel cython numpy | |
| mkdir -p ${{ env.CMAKE_TEMP_DIR }} | |
| cmake -S build_tools/cmake \ | |
| -B ${{ env.CMAKE_TEMP_DIR }} \ | |
| -DCMAKE_BUILD_TYPE=Release \ | |
| -DCMAKE_POSITION_INDEPENDENT_CODE=ON \ | |
| -DBUILD_DEPS=ON \ | |
| -DUSE_PYTHON=ON \ | |
| -DPYTHON_EXECUTABLE_PATH="$PYTHON_BIN" \ | |
| -DPYTHON_INCLUDE_PATH="$PYTHON_INCLUDE" \ | |
| -DPYTHON_LIBRARY_PATH="$PYTHON_LIBRARY" | |
| cmake --build ${{ env.CMAKE_TEMP_DIR }} --config Release --parallel $(nproc) | |
| rm -rf ${{ env.CMAKE_TEMP_DIR }} | |
| $PYTHON_BIN setup.py build_ext --build-lib ${{ env.BUILD_DIR }} --build-temp ${{ env.TEMP_DIR }} sdist bdist_wheel | |
| original=$(ls dist/*.whl) | |
| renamed=$(echo "$original" | sed "s/linux/manylinux_2_28/") | |
| mv "$original" "$renamed" | |
| cp dist/* /output_dist/ | |
| } | |
| export -f build_for_python | |
| mkdir -p /PMMA/dist | |
| pids=() | |
| for tag in $PYTHON_TAGS; do | |
| bash -c "build_for_python $tag" & | |
| pids+=($!) | |
| done | |
| # Wait for all and fail if any fail | |
| for pid in "${pids[@]}"; do | |
| wait "$pid" || exit 1 | |
| done | |
| wait | |
| ' | |
| - name: Upload all Linux wheels | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: wheel-linux-all | |
| path: dist/*.whl | |
| # ────────────────────────────────────── | |
| # 2. Python builds per OS/arch via matrix | |
| # ────────────────────────────────────── | |
| # build_macos: | |
| # strategy: | |
| # matrix: | |
| # include: | |
| # - os: macos | |
| # runs-on: macos-latest | |
| # python-version: '3.8' | |
| # - os: macos | |
| # runs-on: macos-latest | |
| # python-version: '3.9' | |
| # - os: macos | |
| # runs-on: macos-latest | |
| # python-version: '3.10' | |
| # - os: macos | |
| # runs-on: macos-latest | |
| # python-version: '3.11' | |
| # | |
| # runs-on: ${{ matrix.runs-on }} | |
| # steps: | |
| # - uses: actions/checkout@v3 | |
| # | |
| # - name: Setup Python | |
| # uses: actions/setup-python@v5 | |
| # with: | |
| # python-version: ${{ matrix.python-version }} | |
| # | |
| # - name: Install dependencies | |
| # run: brew install cmake | |
| # | |
| # - name: Install Python build tools | |
| # run: | | |
| # python -m pip install --upgrade pip setuptools wheel cython numpy | |
| # | |
| # - name: Configure and build C++ API via CMake | |
| # run: | | |
| # export MACOSX_DEPLOYMENT_TARGET=14.0 | |
| # export CFLAGS="-arch arm64" | |
| # export CXXFLAGS="-arch arm64" | |
| # cmake -S build_tools/cmake -B temporary \ | |
| # -DCMAKE_OSX_ARCHITECTURES=arm64 \ | |
| # -DCMAKE_BUILD_TYPE=Release \ | |
| # -DCMAKE_POSITION_INDEPENDENT_CODE=ON \ | |
| # -DBUILD_DEPS=ON \ | |
| # -DUSE_PYTHON=ON \ | |
| # -DPYTHON_PATH=$(which python) | |
| # cmake --build temporary --config Release --parallel $(sysctl -n hw.ncpu) | |
| # | |
| # - name: Build Python wheel | |
| # run: | | |
| # python setup.py build_ext --build-lib pmma/build --build-temp temporary sdist bdist_wheel --parallel $(sysctl -n hw.ncpu) | |
| # | |
| # - name: Upload wheel | |
| # uses: actions/upload-artifact@v4 | |
| # with: | |
| # name: wheel-${{ matrix.os }}-${{ matrix.python-version }} | |
| # path: dist/*.whl | |
| build_windows: | |
| strategy: | |
| matrix: | |
| include: | |
| - os: windows | |
| runs-on: windows-latest | |
| python-version: '3.8' | |
| - os: windows | |
| runs-on: windows-latest | |
| python-version: '3.9' | |
| - os: windows | |
| runs-on: windows-latest | |
| python-version: '3.10' | |
| - os: windows | |
| runs-on: windows-latest | |
| python-version: '3.11' | |
| runs-on: ${{ matrix.runs-on }} | |
| steps: | |
| - uses: actions/checkout@v3 | |
| - name: Setup Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: ${{ matrix.python-version }} | |
| - name: Install CMake | |
| run: choco install cmake --installargs 'ADD_CMAKE_TO_PATH=System' --yes | |
| - name: Install Python build tools | |
| run: | | |
| python -m pip install --upgrade pip setuptools wheel cython numpy | |
| - name: Configure and build C++ API via CMake | |
| shell: pwsh | |
| run: | | |
| $pythonPath = (Get-Command python).Source | |
| $pythonInclude = (python -c "import sysconfig; print(sysconfig.get_paths()['include'])") | |
| $pythonLib = (python -c "import sysconfig; print(sysconfig.get_config_var('LIBRARY'))") | |
| $pythonLibDir = (python -c "import sysconfig; print(sysconfig.get_config_var('LIBDIR'))") | |
| # Full path to pythonXY.lib | |
| $pythonLibrary = Join-Path $pythonLibDir $pythonLib | |
| cmake -S build_tools/cmake -B temporary ` | |
| -DCMAKE_BUILD_TYPE=Release ` | |
| -DCMAKE_POSITION_INDEPENDENT_CODE=ON ` | |
| -DBUILD_DEPS=ON ` | |
| -DUSE_PYTHON=ON ` | |
| -DPYTHON_EXECUTABLE_PATH="$pythonPath" ` | |
| -DPYTHON_INCLUDE_PATH="$pythonInclude" ` | |
| -DPYTHON_LIBRARY_PATH="$pythonLibrary" | |
| cmake --build temporary --config Release --parallel ((Get-CimInstance Win32_Processor).NumberOfLogicalProcessors) | |
| - name: Build Python wheel | |
| run: | | |
| python setup.py build_ext --build-lib pmma/build --build-temp temporary sdist bdist_wheel | |
| - name: Upload wheel | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: wheel-${{ matrix.os }}-${{ matrix.python-version }} | |
| path: dist/*.whl | |
| # ────────────────────────────────────── | |
| # 3. sdist and publish | |
| # ────────────────────────────────────── | |
| sdist: | |
| name: Build Source Distribution | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v3 | |
| - uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.10' | |
| - run: | | |
| pip install setuptools wheel cython numpy | |
| python setup.py sdist | |
| - uses: actions/upload-artifact@v4 | |
| with: | |
| name: sdist | |
| path: dist/*.tar.gz | |
| publish: | |
| name: Publish to PyPI + GitHub Release | |
| runs-on: ubuntu-latest | |
| needs: [build_windows, sdist, build-core-linux] | |
| steps: | |
| - name: Download artifacts | |
| uses: actions/download-artifact@v4 | |
| with: | |
| path: ./artifacts | |
| - name: Move distribution files | |
| run: | | |
| mkdir -p dist | |
| find artifacts -type f \( -name "*.whl" -o -name "*.tar.gz" \) -exec mv {} dist/ \; | |
| - name: Publish to PyPI | |
| uses: pypa/gh-action-pypi-publish@v1.4.2 | |
| with: | |
| user: __token__ | |
| password: ${{ secrets.PYPI_API_TOKEN }} | |
| packages_dir: dist | |
| - name: Upload to GitHub Release | |
| if: github.event_name == 'release' | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| UPLOAD_URL: ${{ github.event.release.upload_url }} | |
| run: | | |
| for f in dist/*; do | |
| echo "Uploading $f ..." | |
| curl -X POST \ | |
| -H "Authorization: token $GITHUB_TOKEN" \ | |
| -H "Content-Type: application/octet-stream" \ | |
| --data-binary @"$f" \ | |
| "${UPLOAD_URL%\{*}?name=$(basename "$f")" | |
| done |