Skip to content

Workflow file for this run

name: Build and publish Python package
on:
push:
tags:
- 'v*'
workflow_dispatch:
# How cross compilation works with PyO3/maturin-action:
# - For Linux targets, it uses Docker.
# - For other targets, it runs on the host (Docker mostly only supports Linux containers).
# - Even if targeting another platform (e.g. Windows from Linux) was possible, this is likely problematic and suboptimal:
# - Working with libraries and headers for another OS is likely very tricky.
# - Usually the host compilers (e.g. Xcode on macOS, MSVC on Windows) are the most optimal in terms of correctness and performance.
# - It may be against Apple's terms to build macOS binaries from other platforms.
# - On Windows, the action appears to just use the host binary. Cross compilation to a different arch doesn't seem to be yet supported.
# - On macOS, the action appears to be able to target aarch64 just fine despite running on an Intel Mac.
# - We don't need to set up Rust, the action will do so itself.
# - We don't need to set up Python:
# - On macOS, the action auto finds all versions: https://github.com/PyO3/maturin-action/blob/a3013db91b2ef2e51420cfe99ee619c8e72a17e6/src/index.ts#L732
# - On Linux, a Docker container is used which has its own Rust, Python, etc.
# - On Windows, we use the "generate-import-lib" feature on pyo3: https://www.maturin.rs/distribution#cross-compile-to-windows
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-11.0, windows-2019]
variant: ['', '-onepass']
target: [x86_64, aarch64]
python: ['3.8', '3.9', '3.10', '3.11', '3.12']
exclude:
# Building for Windows aarch64 isn't ready yet.
- os: windows-2019
target: aarch64
steps:
- uses: actions/checkout@v1
- name: Patches
shell: bash
run: |
# Maturin uses Cargo.toml name for Python name: https://www.maturin.rs/#usage.
# We edit it here instead of just updating our repo code as Cargo workspace names must be different.
newName=minify-html${{ matrix.variant }}
newName=${newName//-/_}
sed -i.bak 's%^name = ".*$%name = "'$newName'"%' ./minify-html${{ matrix.variant }}-python/Cargo.toml
rm ./minify-html${{ matrix.variant }}-python/Cargo.toml.bak
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.target }}
manylinux: auto
# https://github.com/PyO3/maturin-action/issues/49#issuecomment-1166242843
args: --release --sdist --strip -m ./minify-html${{ matrix.variant }}-python/Cargo.toml -i ${{ matrix.python }}
sccache: true
- name: Install Python build tools (macOS)
if: runner.os == 'macOS'
run: sudo pip install --upgrade twine
- name: Install Python build tools (Linux, Windows)
if: runner.os != 'macOS'
run: pip install --upgrade twine
- name: Pack and publish package
shell: bash
working-directory: ./minify-html${{ matrix.variant }}-python
run: |
cat << 'EOF' > "$HOME/.pypirc"
[pypi]
username = __token__
password = ${{ secrets.PYPI_API_TOKEN }}
EOF
if [[ "$GITHUB_REF" == refs/tags/v* ]]; then
# For idempotency, ignore any existing built wheels that have already been successfully uploaded.
twine upload --skip-existing ../target/wheels/*
else
ls -al ../target/wheels/*
fi