Skip to content

Build and publish the package to PyPI #6

Build and publish the package to PyPI

Build and publish the package to PyPI #6

name: Build and publish the package to PyPI
on:
workflow_dispatch:
inputs:
publish:
description: 'Whether to publish the package (as opposed to just building it)'
required: false
default: true
type: boolean
environment:
description: 'Whether to publish to production PyPI or test PyPI'
required: false
default: prod
type: choice
options: [prod, test]
ref:
description: 'The git ref to build the package for'
required: false
default: ''
type: string
use_lkg:
description: 'Whether to use the last known good versions of dependencies'
required: false
default: True
type: boolean
# annoyingly, there does not seem to be a way to share these input definitions between triggers
workflow_call:
inputs:
publish:
description: 'Whether to publish the package (as opposed to just building it)'
required: false
default: true
type: boolean
# choice type only supported for workflow_dispatch, not workflow_call
environment:
description: 'Whether to publish to production PyPI or test PyPI'
required: false
default: prod
type: string
ref:
description: 'The git ref to build the package for'
required: false
default: ''
type: string
use_lkg:
description: 'Whether to use the last known good versions of dependencies'
required: false
default: True
type: boolean
jobs:
build_wheels:
name: Build wheels on ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
fail-fast: false
runs-on: ${{ matrix.os }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ inputs.ref }}
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: 3.9
- name: Ensure latest pip and setuptools
run: python -m pip install --upgrade pip && pip install --upgrade setuptools
- name: Build wheels
run: pip install 'cibuildwheel < 3' && python -m cibuildwheel --output-dir dist
env:
CIBW_BUILD: ${{ inputs.use_lkg && 'cp3{9,10,11,12,13}-*' || 'cp3*' }}
CIBW_SKIP: "*musl* *win32 *i686"
- name: Upload wheels as artifact
uses: actions/upload-artifact@v4
with:
name: dist-${{ matrix.os }}
path: dist/
build_sdist:
name: Build sdist
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ inputs.ref }}
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: 3.12
- name: Ensure latest pip and setuptools
run: python -m pip install --upgrade pip && pip install --upgrade setuptools
- name: Install econml[all]
run: pip install -e .[all] ${{ inputs.use_lkg && '-r lkg.txt' || '' }}
- name: Build sdist
run: python setup.py sdist
- name: Upload sdist as artifact
uses: actions/upload-artifact@v4
with:
name: dist-sdist
path: dist/
merge:
name: Merge artifacts
needs: [build_wheels, build_sdist]
runs-on: ubuntu-latest
steps:
- name: "Merge artifacts"
uses: actions/upload-artifact/merge@v4
with:
name: dist
pattern: dist-*
delete-merged: true
publish:
name: Publish to PyPI or TestPyPI
needs: [merge]
permissions:
id-token: write
environment: ${{ inputs.environment }}
if: ${{ inputs.publish }}
runs-on: ubuntu-latest
steps:
- name: Download wheels and sdist
uses: actions/download-artifact@v4
with:
name: dist
path: dist/
- name: Upload wheels and sdist to package index
uses: pypa/gh-action-pypi-publish@release/v1
with:
repository-url: ${{ inputs.environment == 'test' && 'https://test.pypi.org/legacy/' || '' }}