Feature/ci cd #17
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: Build and Test Wheels | |
| on: | |
| push: | |
| branches: [main, master] | |
| tags: | |
| - "v*" | |
| pull_request: | |
| branches: [main, master] | |
| release: | |
| types: [published] | |
| # Allow manual triggering or remote triggering via GitHub CLI/API | |
| # (used by cfd repo's version-release.yml via: gh workflow run build-wheels.yml) | |
| workflow_dispatch: | |
| inputs: | |
| cfd_ref: | |
| description: "CFD library ref (tag/branch/commit). Leave empty to auto-detect." | |
| required: false | |
| default: "" | |
| jobs: | |
| build_wheels: | |
| name: Build wheels on ${{ matrix.os }} | |
| runs-on: ${{ matrix.os }} | |
| strategy: | |
| matrix: | |
| os: [ubuntu-latest, windows-latest, macos-latest] | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| # Determine which version of CFD library to use | |
| # Priority: workflow_dispatch input > latest cfd release tag | |
| # This ensures we always build against stable, tested cfd releases | |
| - name: Determine CFD library version | |
| id: cfd-version | |
| shell: bash | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| run: | | |
| # Check for workflow_dispatch input first | |
| if [[ -n "${{ github.event.inputs.cfd_ref }}" ]]; then | |
| echo "ref=${{ github.event.inputs.cfd_ref }}" >> $GITHUB_OUTPUT | |
| echo "Using CFD library ref from input: ${{ github.event.inputs.cfd_ref }}" | |
| else | |
| # Fetch the latest release tag from cfd repo | |
| LATEST_TAG=$(gh api repos/${{ github.repository_owner }}/cfd/releases/latest --jq '.tag_name' 2>/dev/null || echo "") | |
| if [[ -n "$LATEST_TAG" ]]; then | |
| echo "ref=$LATEST_TAG" >> $GITHUB_OUTPUT | |
| echo "Using latest CFD release: $LATEST_TAG" | |
| else | |
| # Fallback to master if no releases exist | |
| echo "ref=master" >> $GITHUB_OUTPUT | |
| echo "No CFD releases found, falling back to master branch" | |
| fi | |
| fi | |
| # Checkout the CFD C library into 'src/cfd_lib' subdirectory | |
| # This location is inside the source tree so it's accessible from cibuildwheel | |
| # build environments (including Linux Docker containers where source is mounted) | |
| - name: Checkout CFD C library | |
| uses: actions/checkout@v4 | |
| with: | |
| repository: ${{ github.repository_owner }}/cfd | |
| path: src/cfd_lib | |
| ref: ${{ steps.cfd-version.outputs.ref }} | |
| fetch-depth: 0 | |
| # Build wheels using cibuildwheel | |
| # The before-build commands in pyproject.toml will build the CFD library | |
| - name: Build wheels | |
| uses: pypa/cibuildwheel@v2.21.3 | |
| env: | |
| # CIBW_ENVIRONMENT completely replaces (not merges with) the environment | |
| # settings in pyproject.toml, including platform-specific ones. | |
| # We must specify all required variables here for CI builds. | |
| # CFD_ROOT=./src/cfd_lib points to the cfd checkout inside source tree. | |
| CIBW_ENVIRONMENT: "CMAKE_BUILD_TYPE=Release CFD_STATIC_LINK=ON CFD_USE_STABLE_ABI=ON CFD_ROOT=./src/cfd_lib" | |
| - uses: actions/upload-artifact@v4 | |
| with: | |
| name: wheels-${{ matrix.os }} | |
| path: ./wheelhouse/*.whl | |
| build_sdist: | |
| name: Build source distribution | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Build sdist | |
| run: pipx run build --sdist | |
| - uses: actions/upload-artifact@v4 | |
| with: | |
| name: sdist | |
| path: dist/*.tar.gz | |
| test_package: | |
| name: Test package installation | |
| needs: [build_wheels] | |
| runs-on: ${{ matrix.os }} | |
| strategy: | |
| matrix: | |
| os: [ubuntu-latest, windows-latest, macos-latest] | |
| python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - uses: actions/setup-python@v5 | |
| with: | |
| python-version: ${{ matrix.python-version }} | |
| - uses: actions/download-artifact@v4 | |
| with: | |
| name: wheels-${{ matrix.os }} | |
| path: wheelhouse | |
| - name: Install wheel and test dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| python -m pip install --find-links wheelhouse cfd-python | |
| python -m pip install pytest numpy | |
| - name: Run full test suite | |
| run: pytest tests/ -v | |
| upload_pypi: | |
| name: Upload to PyPI | |
| needs: [build_wheels, build_sdist, test_package] | |
| runs-on: ubuntu-latest | |
| # Publish on: release event, tag push (v*), or workflow_dispatch on a tag | |
| if: | | |
| github.event_name == 'release' || | |
| (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')) || | |
| (github.event_name == 'workflow_dispatch' && startsWith(github.ref, 'refs/tags/v')) | |
| environment: | |
| name: pypi | |
| url: https://pypi.org/p/cfd-python | |
| permissions: | |
| id-token: write | |
| steps: | |
| - uses: actions/download-artifact@v4 | |
| with: | |
| pattern: wheels-* | |
| path: dist | |
| merge-multiple: true | |
| - uses: actions/download-artifact@v4 | |
| with: | |
| name: sdist | |
| path: dist | |
| - name: Publish to PyPI | |
| uses: pypa/gh-action-pypi-publish@v1.12.4 # Pin to specific version for security | |
| with: | |
| packages-dir: dist/ | |
| upload_test_pypi: | |
| name: Upload to Test PyPI | |
| needs: [build_wheels, build_sdist, test_package] | |
| runs-on: ubuntu-latest | |
| if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master') | |
| environment: | |
| name: testpypi | |
| url: https://test.pypi.org/p/cfd-python | |
| permissions: | |
| id-token: write | |
| steps: | |
| - uses: actions/download-artifact@v4 | |
| with: | |
| pattern: wheels-* | |
| path: dist | |
| merge-multiple: true | |
| - uses: actions/download-artifact@v4 | |
| with: | |
| name: sdist | |
| path: dist | |
| - name: Publish to Test PyPI | |
| uses: pypa/gh-action-pypi-publish@v1.12.4 # Pin to specific version for security | |
| with: | |
| repository-url: https://test.pypi.org/legacy/ | |
| packages-dir: dist/ |