|
| 1 | +# SPDX-FileCopyrightText: Contributors to the Power Grid Model project <[email protected]> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MPL-2.0 |
| 4 | + |
| 5 | + |
| 6 | +name: Build, Test, Sonar |
| 7 | + |
| 8 | +on: |
| 9 | + push: |
| 10 | + branches: |
| 11 | + - main |
| 12 | + # run pipeline on pull request |
| 13 | + pull_request: |
| 14 | + # run pipeline on merge queue |
| 15 | + merge_group: |
| 16 | + # run pipeline from another workflow |
| 17 | + workflow_call: |
| 18 | + inputs: |
| 19 | + create_release: |
| 20 | + type: boolean |
| 21 | + description: Create a (pre-)release when CI passes |
| 22 | + default: false |
| 23 | + required: false |
| 24 | + # run this workflow manually from the Actions tab |
| 25 | + workflow_dispatch: |
| 26 | + inputs: |
| 27 | + create_release: |
| 28 | + type: boolean |
| 29 | + description: Create a (pre-)release when CI passes |
| 30 | + default: false |
| 31 | + required: true |
| 32 | + |
| 33 | +concurrency: |
| 34 | + group: ${{ github.workflow }}-${{ github.ref }}-main |
| 35 | + cancel-in-progress: true |
| 36 | + |
| 37 | +jobs: |
| 38 | + |
| 39 | + build-python: |
| 40 | + runs-on: ubuntu-latest |
| 41 | + outputs: |
| 42 | + version: ${{ steps.version.outputs.version }} |
| 43 | + steps: |
| 44 | + |
| 45 | + - name: Checkout source code |
| 46 | + uses: actions/checkout@v4 |
| 47 | + |
| 48 | + - name: Setup Python 3.13 |
| 49 | + uses: actions/setup-python@v5 |
| 50 | + with: |
| 51 | + python-version: "3.13" |
| 52 | + |
| 53 | + - name: Set PyPI version |
| 54 | + uses: PowerGridModel/pgm-version-bump@main |
| 55 | + with: |
| 56 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 57 | + |
| 58 | + - name: Build |
| 59 | + run: | |
| 60 | + cat PYPI_VERSION |
| 61 | + pip install build |
| 62 | + python -m build --outdir wheelhouse . |
| 63 | +
|
| 64 | + - name: Save version |
| 65 | + id: version |
| 66 | + run: echo "version=$(cat PYPI_VERSION)" >> $GITHUB_OUTPUT |
| 67 | + |
| 68 | + - name: Store built wheel file |
| 69 | + uses: actions/upload-artifact@v4 |
| 70 | + with: |
| 71 | + name: power-grid-model-io |
| 72 | + path: wheelhouse/ |
| 73 | + |
| 74 | + sonar-cloud: |
| 75 | + permissions: |
| 76 | + contents: write |
| 77 | + runs-on: ubuntu-latest |
| 78 | + steps: |
| 79 | + |
| 80 | + - name: Checkout source code |
| 81 | + uses: actions/checkout@v4 |
| 82 | + with: |
| 83 | + fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis |
| 84 | + |
| 85 | + - name: Setup Python 3.11 |
| 86 | + uses: actions/setup-python@v5 |
| 87 | + with: |
| 88 | + python-version: "3.11" |
| 89 | + |
| 90 | + - name: Install in develop mode |
| 91 | + run: | |
| 92 | + pip install -e .[dev] |
| 93 | +
|
| 94 | + - name: Test and Coverage |
| 95 | + run: | |
| 96 | + pytest --cov-report=xml:coverage.xml --cov-fail-under=0 |
| 97 | +
|
| 98 | + # Fix relative paths in coverage file |
| 99 | + # Known bug: https://community.sonarsource.com/t/sonar-on-github-actions-with-python-coverage-source-issue/36057 |
| 100 | + sed -i 's@/home/runner/work/power-grid-model-io/power-grid-model-io@/github/workspace@g' coverage.xml |
| 101 | +
|
| 102 | + - name: SonarCloud Scan |
| 103 | + if: ${{ (github.event_name == 'push') || (github.event.pull_request.head.repo.owner.login == 'PowerGridModel') }} |
| 104 | + uses: SonarSource/sonarqube-scan-action@master |
| 105 | + env: |
| 106 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 107 | + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
| 108 | + |
| 109 | + unit-tests: |
| 110 | + needs: build-python |
| 111 | + strategy: |
| 112 | + matrix: |
| 113 | + os: [ubuntu-latest, macos-latest, windows-latest] |
| 114 | + python: ["3.11", "3.12"] |
| 115 | + fail-fast: false |
| 116 | + runs-on: ${{ matrix.os }} |
| 117 | + |
| 118 | + steps: |
| 119 | + |
| 120 | + - name: Checkout source code |
| 121 | + uses: actions/checkout@v4 |
| 122 | + |
| 123 | + - name: Setup Python ${{ matrix.python }} |
| 124 | + uses: actions/setup-python@v5 |
| 125 | + with: |
| 126 | + python-version: ${{ matrix.python }} |
| 127 | + |
| 128 | + - name: Load built wheel file |
| 129 | + uses: actions/download-artifact@v4 |
| 130 | + with: |
| 131 | + name: power-grid-model-io |
| 132 | + path: wheelhouse/ |
| 133 | + |
| 134 | + - name: Install built wheel file |
| 135 | + run: pip install power-grid-model-io[dev]==${{ needs.build-python.outputs.version }} --find-links=wheelhouse |
| 136 | + |
| 137 | + - name: Unit test and coverage |
| 138 | + run: pytest --verbose |
| 139 | + |
| 140 | + validation-tests: |
| 141 | + needs: build-python |
| 142 | + strategy: |
| 143 | + matrix: |
| 144 | + os: [ubuntu-latest, macos-latest, windows-latest] |
| 145 | + python: ["3.11", "3.12"] |
| 146 | + fail-fast: false |
| 147 | + runs-on: ${{ matrix.os }} |
| 148 | + |
| 149 | + steps: |
| 150 | + |
| 151 | + - name: Checkout source code |
| 152 | + uses: actions/checkout@v4 |
| 153 | + |
| 154 | + - name: Setup Python ${{ matrix.python }} |
| 155 | + uses: actions/setup-python@v5 |
| 156 | + with: |
| 157 | + python-version: ${{ matrix.python }} |
| 158 | + |
| 159 | + - name: Load built wheel file |
| 160 | + uses: actions/download-artifact@v4 |
| 161 | + with: |
| 162 | + name: power-grid-model-io |
| 163 | + path: wheelhouse/ |
| 164 | + |
| 165 | + - name: Install built wheel file |
| 166 | + run: pip install power-grid-model-io[dev]==${{ needs.build-python.outputs.version }} --find-links=wheelhouse |
| 167 | + |
| 168 | + - name: Validation tests |
| 169 | + run: pytest tests/validation --no-cov --verbose |
| 170 | + |
| 171 | + github-release: |
| 172 | + needs: |
| 173 | + - build-python |
| 174 | + - unit-tests |
| 175 | + - validation-tests |
| 176 | + - sonar-cloud |
| 177 | + permissions: |
| 178 | + contents: write |
| 179 | + runs-on: ubuntu-latest |
| 180 | + steps: |
| 181 | + - name: Setup Python 3.13 |
| 182 | + uses: actions/setup-python@v5 |
| 183 | + with: |
| 184 | + python-version: "3.13" |
| 185 | + |
| 186 | + - name: Load built wheel file |
| 187 | + uses: actions/download-artifact@v4 |
| 188 | + with: |
| 189 | + name: power-grid-model-io |
| 190 | + path: wheelhouse/ |
| 191 | + |
| 192 | + - name: Get tag |
| 193 | + id: tag |
| 194 | + run: echo "tag=v${{ needs.build-python.outputs.version }}" >> $GITHUB_OUTPUT |
| 195 | + |
| 196 | + - name: Display tag |
| 197 | + run: echo "${{ steps.tag.outputs.tag }}" |
| 198 | + |
| 199 | + - name: Create GitHub release |
| 200 | + if: (github.event_name == 'push') || ((github.event_name == 'workflow_dispatch') && (github.event.inputs.create_release == 'true')) |
| 201 | + uses: softprops/action-gh-release@v2 |
| 202 | + with: |
| 203 | + files: | |
| 204 | + ./wheelhouse/* |
| 205 | + tag_name: "${{ steps.tag.outputs.tag }}" |
| 206 | + prerelease: ${{github.ref != 'refs/heads/main'}} |
| 207 | + generate_release_notes: true |
| 208 | + target_commitish: ${{ github.sha }} |
0 commit comments