add default versions for os and pyversion #1
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: cd-pypi | ||
on: | ||
workflow_call: | ||
inputs: | ||
testpypi: | ||
description: Whether to upload to testpypi instead of pypi. | ||
Requires secrets.PYPI_TEST_API_TOKEN to be defined. | ||
type: boolean | ||
required: false | ||
default: false | ||
working-directory: | ||
description: Working directory to build Python package (for monorepos). | ||
Defaults to root directory. | ||
type: string | ||
required: false | ||
default: "./" | ||
platforms: | ||
description: Platforms to build for e.g. "['ubuntu-latest','macos-latest','windows-latest']" | ||
required: false | ||
type: string | ||
default: '' | ||
pyversions: | ||
description: Python versions to build for e.g. "['38','39','310','311','312']" | ||
required: false | ||
type: string | ||
default: '' | ||
build_args: | ||
description: Args to pass in to build as `python -m build {build_args}` e.g. '--sdist' | ||
type: string | ||
required: false | ||
default: '' | ||
jobs: | ||
deploy_source: | ||
if: ${{ github.ref_type == 'tag' || github.event_name == 'release' || inputs.testpypi }} | ||
runs-on: ubuntu-latest | ||
defaults: | ||
run: | ||
working-directory: ${{ inputs.working-directory }} | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: "0" | ||
- name: Test tag name pattern | ||
if: ${{ !inputs.testpypi }} | ||
shell: python | ||
run: | | ||
import re | ||
import sys | ||
tag_pattern = r"${{ vars.TAG_REGEX_FOR_DEPLOYMENT }}" | ||
ref = "${{ github.ref_name }}" | ||
if not tag_pattern: | ||
sys.exit(0) | ||
if not re.match(tag_pattern, ref): | ||
print(f"::error::{ref} does not match {tag_pattern}") | ||
sys.exit(1) | ||
- name: Test branch name pattern | ||
if: ${{ vars.BRANCH_REGEX_FOR_DEPLOYMENT != '' && !inputs.testpypi }} | ||
run: | | ||
echo "Checking that this release is being made from a main/master branch ( ${{ vars.BRANCH_REGEX_FOR_DEPLOYMENT }} ) - will fail if not" | ||
git branch --all --list --format "%(refname:lstrip=-1)" --contains "${{ github.ref_name }}" | grep -E "${{ vars.BRANCH_REGEX_FOR_DEPLOYMENT }}" | ||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: "3.x" | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install setuptools wheel twine build | ||
- name: Check version | ||
run: | | ||
if [ -f "setup.py" ]; then | ||
if [ -f "pyproject.toml" ] ; then | ||
# we need to install eg setuptools_scm to correctly determine version later | ||
extra_deps=$(python -c 'import tomllib; print(" ".join(f"{e}" for e in tomllib.load(open("pyproject.toml", "rb"))["build-system"]["requires"]))') | ||
pip install --upgrade $extra_deps | ||
fi | ||
version=$(python setup.py --version) | ||
release=${{ github.ref_name }} | ||
if [ "${{ inputs.testpypi }}" != "true" ] ; then test "$release" == "$version"; fi | ||
fi | ||
- name: Build and publish to pypi | ||
if: ${{ !inputs.testpypi }} | ||
env: | ||
TWINE_USERNAME: __token__ | ||
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} | ||
run: | | ||
python -m build ${{ inputs.build_args }} | ||
twine check dist/* | ||
twine upload dist/* | ||
- name: Build and publish to testpypi | ||
if: ${{ inputs.testpypi }} | ||
env: | ||
TWINE_USERNAME: __token__ | ||
TWINE_PASSWORD: ${{ secrets.PYPI_TEST_API_TOKEN }} | ||
run: | | ||
python -m build ${{ inputs.build_args }} | ||
twine check dist/* | ||
# HINT: if your upload fails here due to "unsupported local version", | ||
# put `local_scheme = "no-local-version"` pyproject.toml's | ||
# [tools.setuptools_scm] | ||
twine upload --repository testpypi --verbose dist/* | ||
deploy_wheels: | ||
if: ${{ github.ref_type == 'tag' || github.event_name == 'release' || inputs.testpypi || inputs.platforms!='' || inputs.pyversions!='' }} | ||
needs: deploy_source | ||
name: Build wheels and source distribution | ||
strategy: | ||
matrix: | ||
os: ${{ fromJson(inputs.platforms != '' ? inputs.platforms : '["ubuntu-latest"]') }} | ||
python: ${{ fromJson(inputs.platforms != '' ? inputs.platforms : '["310"]') }} | ||
runs-on: ${{ matrix.os }} | ||
defaults: | ||
run: | ||
working-directory: ${{ inputs.working-directory }} | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: "0" | ||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: "3.x" | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install setuptools wheel twine build cibuildwheel | ||
- name: Build wheels | ||
run: | | ||
cibuildwheel --output-dir dist | ||
env: | ||
CIBW_BUILD: "cp${{ matrix.python }}-*" | ||
CIBW_BUILD_VERBOSITY: 1 | ||
- name: Upload to PyPI | ||
if: ${{ !inputs.testpypi }} | ||
env: | ||
TWINE_USERNAME: __token__ | ||
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} | ||
run: | | ||
pip install twine | ||
twine check dist/*.whl | ||
twine upload dist/*.whl | ||
- name: Upload to test-PyPI | ||
if: ${{ inputs.testpypi }} | ||
env: | ||
TWINE_USERNAME: __token__ | ||
TWINE_PASSWORD: ${{ secrets.PYPI_TEST_API_TOKEN }} | ||
run: | | ||
pip install twine | ||
twine check dist/*.whl | ||
# HINT: if your upload fails here due to "unsupported local version", | ||
# put `local_scheme = "no-local-version"` pyproject.toml's | ||
# [tools.setuptools_scm] | ||
twine upload --repository testpypi --verbose dist/*.whl |