From afb73b3918f378ade58c9b31d4bf9fa9480415a2 Mon Sep 17 00:00:00 2001 From: David Vo Date: Sun, 18 Jan 2026 02:46:19 +1100 Subject: [PATCH] refactor: separate build, test, and deploy in Python workflow (#2308) _Test what you build, deploy what you test._ This refactors the Python CI workflow to wait for _all_ tests to pass before publishing photonlibpy to PyPI. - build-python-examples reuses the built wheel, removes redundant builds - Simplify run.sh to not rebuild wheel since it's already installed --- .github/workflows/python.yml | 100 ++++++++++++++++++++----------- photonlib-python-examples/run.sh | 13 ---- 2 files changed, 65 insertions(+), 48 deletions(-) diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index 811fad63b..cc45cee79 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -12,7 +12,7 @@ concurrency: cancel-in-progress: true jobs: - build-and-deploy: + build-py: runs-on: ubuntu-24.04 steps: @@ -29,38 +29,57 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install setuptools wheel pytest mypy + pip install setuptools wheel - name: Build wheel working-directory: ./photon-lib/py run: python setup.py sdist bdist_wheel - - name: Run Unit Tests - working-directory: ./photon-lib/py - run: | - pip install --no-cache-dir dist/*.whl - pytest - - - name: Run mypy type checking - run: mypy --show-column-numbers --config-file photon-lib/py/pyproject.toml photon-lib - - name: Upload artifacts uses: actions/upload-artifact@v6 with: name: dist path: ./photon-lib/py/dist/ - - name: Publish package distributions to TestPyPI - # Only upload on tags - if: startsWith(github.ref, 'refs/tags/v') - uses: pypa/gh-action-pypi-publish@release/v1 - with: - packages-dir: ./photon-lib/py/dist/ + test-py: + needs: build-py + runs-on: ubuntu-24.04 - permissions: - id-token: write # IMPORTANT: this permission is mandatory for trusted publishing + steps: + - name: Checkout code + uses: actions/checkout@v6 + with: + fetch-depth: 0 + + - name: Set up Python + uses: actions/setup-python@v6 + with: + python-version: 3.14 + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pytest mypy + + - name: Download artifacts + uses: actions/download-artifact@v6 + with: + name: dist + path: dist/ + + - name: Install package + shell: bash + run: pip install --no-cache-dir dist/*.whl + + - name: Run Unit Tests + shell: bash + run: pytest --import-mode=importlib photon-lib/py/test/ + + - name: Run mypy type checking + run: mypy --show-column-numbers --config-file photon-lib/py/pyproject.toml photon-lib build-python-examples: + needs: build-py strategy: matrix: os: [ubuntu-24.04, windows-2022, macos-14] @@ -81,28 +100,18 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install setuptools wheel pytest mypy - - name: Build wheel - working-directory: ./photon-lib/py - run: python setup.py sdist bdist_wheel + - name: Download artifacts + uses: actions/download-artifact@v6 + with: + name: dist + path: ./photon-lib/py/dist/ - - name: Build and configure PhotonLibPy - working-directory: ./photon-lib/py - shell: bash - run: | - ./buildAndTest.sh - ./enableUsingDevBuilds.sh - - - name: Run Unit Tests + - name: Install PhotonLibPy package working-directory: ./photon-lib/py shell: bash run: | pip install --no-cache-dir dist/*.whl - pytest - - - name: Run mypy type checking - run: mypy --show-column-numbers --config-file photon-lib/py/pyproject.toml photon-lib - name: Build Python examples working-directory: photonlib-python-examples @@ -113,3 +122,24 @@ jobs: echo $folder ./run.sh $folder done + + deploy: + needs: [test-py, build-python-examples] + runs-on: ubuntu-24.04 + # Only upload on tags + if: startsWith(github.ref, 'refs/tags/v') + + steps: + - name: Download artifacts + uses: actions/download-artifact@v6 + with: + name: dist + path: dist/ + + - name: Publish package distributions to PyPI + uses: pypa/gh-action-pypi-publish@release/v1 + with: + packages-dir: ./dist/ + + permissions: + id-token: write # IMPORTANT: this permission is mandatory for trusted publishing diff --git a/photonlib-python-examples/run.sh b/photonlib-python-examples/run.sh index d7ff04a54..e6838b135 100755 --- a/photonlib-python-examples/run.sh +++ b/photonlib-python-examples/run.sh @@ -5,19 +5,6 @@ if [ $# -eq 0 ] exit 1 fi -# To run any example, we want to use photonlib out of this repo -# Build the wheel first -pushd ../photon-lib/py -if [ -d build ] - then rm -rdf build -fi -python3 setup.py bdist_wheel -popd - -# Add the output directory to PYTHONPATH to make sure it gets picked up -export PHOTONLIBPY_ROOT=../photon-lib/py -export PYTHONPATH=$PHOTONLIBPY_ROOT - # Move to the right example folder cd $1