| 1 |
name: Build and Publish Python Package |
| 2 |
|
| 3 |
# Be very careful granting extra permissions here, as this workflow uses third party actions. |
| 4 |
# Prefer to pin to exact commits for third-party actions. I'm making an exception for actions |
| 5 |
# maintained by GitHub itself. |
| 6 |
|
| 7 |
# For now, just trigger this workflow manually. |
| 8 |
on: |
| 9 |
workflow_dispatch: |
| 10 |
inputs: |
| 11 |
upload_to_pypi: |
| 12 |
description: "Upload generate package files to PyPI" |
| 13 |
required: true |
| 14 |
type: boolean |
| 15 |
pypi_environment: |
| 16 |
description: "Which PyPI instance to publish to" |
| 17 |
required: true |
| 18 |
type: choice |
| 19 |
options: |
| 20 |
- testpypi |
| 21 |
- pypi |
| 22 |
|
| 23 |
jobs: |
| 24 |
build_sdist: |
| 25 |
name: Build Python sdist |
| 26 |
runs-on: ubuntu-24.04 |
| 27 |
permissions: |
| 28 |
contents: read |
| 29 |
|
| 30 |
steps: |
| 31 |
- uses: actions/checkout@v4 |
| 32 |
- uses: actions/setup-python@v5 |
| 33 |
with: |
| 34 |
python-version: "3.8" |
| 35 |
cache: "pip" |
| 36 |
- run: pip install 'build==1.2.2' |
| 37 |
- name: Build sdist |
| 38 |
run: python3 -m build --sdist |
| 39 |
- uses: actions/upload-artifact@v4 |
| 40 |
with: |
| 41 |
name: sdist |
| 42 |
path: ./dist/*.gz |
| 43 |
if-no-files-found: error |
| 44 |
|
| 45 |
build_wheels: |
| 46 |
name: Build wheels on ${{ matrix.os }} |
| 47 |
runs-on: ${{ matrix.os }} |
| 48 |
strategy: |
| 49 |
fail-fast: false |
| 50 |
matrix: |
| 51 |
os: [ubuntu-22.04, windows-latest, macos-14, macos-latest] |
| 52 |
|
| 53 |
permissions: |
| 54 |
contents: read |
| 55 |
|
| 56 |
steps: |
| 57 |
- uses: actions/checkout@v4 |
| 58 |
|
| 59 |
- name: Build wheels |
| 60 |
uses: pypa/cibuildwheel@298ed2fb2c105540f5ed055e8a6ad78d82dd3a7e # v3.3.1 |
| 61 |
env: |
| 62 |
# CIBuildWheel itself no longer supports earlier than Python 3.8, which is |
| 63 |
# the earliest we try to support here. Also likely cibuildwheel will soon |
| 64 |
# drop support for 3.8, see https://github.com/pypa/cibuildwheel/issues/2685 |
| 65 |
CIBW_SKIP: "*-win32 *-manylinux_i686 *-musllinux_i686" |
| 66 |
CIBW_TEST_COMMAND: > |
| 67 |
python {package}/python/_jsonnet_test.py |
| 68 |
|
| 69 |
- uses: actions/upload-artifact@v4 |
| 70 |
with: |
| 71 |
name: cibw-wheels-${{ matrix.os }} |
| 72 |
path: ./wheelhouse/*.whl |
| 73 |
if-no-files-found: error |
| 74 |
|
| 75 |
upload_to_pypi: |
| 76 |
name: "Upload packages to PyPI" |
| 77 |
needs: [build_sdist, build_wheels] |
| 78 |
runs-on: ubuntu-24.04 |
| 79 |
if: "${{ inputs.upload_to_pypi }}" |
| 80 |
permissions: |
| 81 |
contents: read |
| 82 |
id-token: write # Needed for PyPI Trusted Publishing |
| 83 |
environment: |
| 84 |
name: "${{ inputs.pypi_environment }}" |
| 85 |
url: "${{ inputs.pypi_environment == 'pypi' && 'https://pypi.org/p/jsonnet' || 'https://test.pypi.org/p/jsonnet' }}" |
| 86 |
steps: |
| 87 |
- uses: actions/download-artifact@v4 |
| 88 |
with: |
| 89 |
path: cibw-wheels |
| 90 |
pattern: cibw-wheels-* |
| 91 |
- uses: actions/download-artifact@v4 |
| 92 |
with: |
| 93 |
path: sdist |
| 94 |
name: sdist |
| 95 |
- name: Flatten wheels to one directory |
| 96 |
run: | |
| 97 |
mkdir dist |
| 98 |
find cibw-wheels/ -type f -name '*.whl' -exec mv '{}' ./dist/ ';' |
| 99 |
find sdist/ -type f -name '*.gz' -exec mv '{}' ./dist/ ';' |
| 100 |
- name: Publish to PyPI |
| 101 |
uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # v1.13.0 |
| 102 |
with: |
| 103 |
verbose: true |
| 104 |
print-hash: true |
| 105 |
repository-url: "${{ inputs.pypi_environment == 'testpypi' && 'https://test.pypi.org/legacy/' || '' }}" |