From 486ac57e4cb3dac8f7a29945d1e332996748d562 Mon Sep 17 00:00:00 2001 From: Vytautas Liuolia Date: Thu, 29 Aug 2024 18:16:53 +0200 Subject: [PATCH] chore: flesh out cibuildwheel workflow --- .github/workflows/cibuildwheel.yaml | 97 ++++++++++++++++++++++++---- .github/workflows/create-wheels.yaml | 5 +- tools/check_dist.py | 70 ++++++++++++++++++++ 3 files changed, 156 insertions(+), 16 deletions(-) create mode 100755 tools/check_dist.py diff --git a/.github/workflows/cibuildwheel.yaml b/.github/workflows/cibuildwheel.yaml index b3ec82cfa..9da120605 100644 --- a/.github/workflows/cibuildwheel.yaml +++ b/.github/workflows/cibuildwheel.yaml @@ -9,6 +9,11 @@ on: # NOTE(vytas): Also allow to release to Test PyPi manually. workflow_dispatch: + # TODO(vytas): Remove later (temporary testing). + pull_request: + branches: + - master + jobs: build-sdist: name: sdist @@ -27,7 +32,8 @@ jobs: - name: Build sdist run: | - pip install build + pip install --upgrade pip + pip install --upgrade build python -m build --sdist - name: Upload artifacts @@ -36,6 +42,13 @@ jobs: name: cibw-sdist path: dist/*.tar.gz + - name: Nuclear testing + run: | + echo ${{ github.event_name }} + echo ${{ github.event_name == 'release' }} + echo ${{ github.ref }} + python -c '0/0' + build-wheels: name: ${{ matrix.python }}-${{ matrix.platform.name }} needs: build-sdist @@ -107,35 +120,93 @@ jobs: name: cibw-wheel-${{ matrix.python }}-${{ matrix.platform.name }} path: wheelhouse/*.whl + publish-sdist: + name: publish-sdist + needs: + - build-sdist + - build-wheels + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 2 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.12" + + - name: Download artifacts + uses: actions/download-artifact@v4 + with: + pattern: cibw-sdist + path: dist + + - name: Check collected artifacts + run: | + python tools/check_dist.py --dist-dir dist/ + + - name: Upload sdist to release + uses: AButler/upload-release-assets@v2.0 + if: github.event_name == 'release' + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + files: 'dist/*.tar.gz' + + - name: Publish sdist to TestPyPI + uses: pypa/gh-action-pypi-publish@release/v1 + if: github.event_name == 'workflow_dispatch' + with: + password: ${{ secrets.TEST_PYPI_TOKEN }} + repository-url: https://test.pypi.org/legacy/ + + - name: Publish sdist to PyPI + uses: pypa/gh-action-pypi-publish@release/v1 + if: github.event_name == 'release' + with: + password: ${{ secrets.PYPI_TOKEN }} + publish-wheels: - name: publish + name: publish-wheels needs: - build-sdist - build-wheels + - publish-sdist runs-on: ubuntu-latest steps: - - name: Download packages + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 2 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.12" + + - name: Download artifacts uses: actions/download-artifact@v4 with: - pattern: cibw-* + pattern: cibw-wheel-* path: dist merge-multiple: true - name: Check collected artifacts - # TODO(vytas): Run a script to perform version sanity checks instead. - run: ls -l dist/ + run: | + python tools/check_dist.py --dist-dir dist/ - - name: Publish artifacts to TestPyPI + - name: Publish wheels to TestPyPI uses: pypa/gh-action-pypi-publish@release/v1 if: github.event_name == 'workflow_dispatch' with: password: ${{ secrets.TEST_PYPI_TOKEN }} repository-url: https://test.pypi.org/legacy/ - # TODO(vytas): Enable this nuclear option once happy with other tests. - # - name: Publish artifacts to PyPI - # uses: pypa/gh-action-pypi-publish@release/v1 - # if: github.event_name == 'release' - # with: - # password: ${{ secrets.PYPI_TOKEN }} + - name: Publish wheels to PyPI + uses: pypa/gh-action-pypi-publish@release/v1 + if: github.event_name == 'release' + with: + password: ${{ secrets.PYPI_TOKEN }} diff --git a/.github/workflows/create-wheels.yaml b/.github/workflows/create-wheels.yaml index 6caf8c4df..23ee6d6f4 100644 --- a/.github/workflows/create-wheels.yaml +++ b/.github/workflows/create-wheels.yaml @@ -1,9 +1,8 @@ name: Create wheel on: - # run when a release has been created - release: - types: [created] + # TODO(vytas): Phase out this workflow in favour of cibuildwheel.yaml. + workflow_dispatch: env: # set this so the falcon test uses the installed version and not the local one diff --git a/tools/check_dist.py b/tools/check_dist.py new file mode 100755 index 000000000..2b8e54fe1 --- /dev/null +++ b/tools/check_dist.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python + +import argparse +import pathlib +import sys + +HERE = pathlib.Path(__file__).resolve().parent +DIST = HERE.parent / 'dist' + + +def check_dist(dist): + sdist = None + versions = set() + wheels = [] + + for path in dist.iterdir(): + if not path.is_file(): + continue + + if path.name.endswith('.tar.gz'): + assert sdist is None, f'sdist already exists: {sdist}' + sdist = path.name + + elif path.name.endswith('.whl'): + wheels.append(path.name) + + else: + sys.stderr.write(f'Unexpected file found in dist: {path.name}\n') + sys.exit(1) + + package, _, _ = path.stem.partition('.tar') + falcon, version, *_ = package.split('-') + assert falcon == 'falcon', 'Unexpected package name: {path.name}' + versions.add(version) + + if not versions: + sys.stderr.write('No artifacts collected!\n') + sys.exit(1) + if len(versions) > 1: + sys.stderr.write(f'Multiple versions found: {tuple(versions)}!\n') + sys.exit(1) + version = versions.pop() + + wheel_list = ' None\n' + if wheels: + wheel_list = ''.join(f' {wheel}\n' for wheel in sorted(wheels)) + + print(f'[{dist}]\n') + print(f'sdist found:\n {sdist}\n') + print(f'wheels found:\n{wheel_list}') + print(f'version identified:\n {version}\n') + + +def main(): + description = 'Check artifacts (sdist, wheels) inside dist dir.' + + parser = argparse.ArgumentParser(description=description) + parser.add_argument( + '-d', + '--dist-dir', + default=str(DIST), + help='dist directory to check (default: %(default)s)', + ) + + args = parser.parse_args() + check_dist(pathlib.Path(args.dist_dir)) + + +if __name__ == '__main__': + main()