|
| 1 | +name: .NET CI with Code Coverage |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ "main" ] |
| 6 | + pull_request: |
| 7 | + branches: [ "main" ] |
| 8 | + |
| 9 | +jobs: |
| 10 | + build-and-test: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + permissions: |
| 13 | + contents: read |
| 14 | + checks: write |
| 15 | + |
| 16 | + steps: |
| 17 | + - uses: actions/checkout@v4 |
| 18 | + |
| 19 | + - name: Setup .NET |
| 20 | + uses: actions/setup-dotnet@v4 |
| 21 | + with: |
| 22 | + dotnet-version: 9.0.x |
| 23 | + |
| 24 | + - name: Restore dependencies |
| 25 | + run: dotnet restore |
| 26 | + |
| 27 | + - name: Build |
| 28 | + run: dotnet build --no-restore --configuration Release |
| 29 | + |
| 30 | + - name: Test |
| 31 | + run: dotnet test --no-build --configuration Release --verbosity normal --collect:"XPlat Code Coverage" --results-directory ./TestResults |
| 32 | + |
| 33 | + - name: Upload coverage reports to Codecov |
| 34 | + uses: codecov/codecov-action@v5 |
| 35 | + with: |
| 36 | + directory: ./TestResults |
| 37 | + fail_ci_if_error: false |
| 38 | + verbose: true |
| 39 | + env: |
| 40 | + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} |
| 41 | + |
| 42 | + - name: Generate Coverage Report |
| 43 | + uses: danielpalme/ReportGenerator-GitHub-Action@5.4.2 |
| 44 | + with: |
| 45 | + reports: 'TestResults/**/coverage.cobertura.xml' |
| 46 | + targetdir: 'TestResults/CoverageReport' |
| 47 | + reporttypes: 'Html;TextSummary;Cobertura;Badges' |
| 48 | + |
| 49 | + - name: Display Coverage Summary |
| 50 | + run: | |
| 51 | + echo "## Code Coverage Summary" >> $GITHUB_STEP_SUMMARY |
| 52 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 53 | + cat TestResults/CoverageReport/Summary.txt >> $GITHUB_STEP_SUMMARY |
| 54 | + |
| 55 | + - name: Upload Coverage Report as Artifact |
| 56 | + uses: actions/upload-artifact@v4 |
| 57 | + if: always() |
| 58 | + with: |
| 59 | + name: coverage-report |
| 60 | + path: TestResults/CoverageReport |
| 61 | + retention-days: 30 |
| 62 | + |
| 63 | + - name: Check Coverage Threshold |
| 64 | + run: | |
| 65 | + COVERAGE=$(grep -oP 'Line coverage: \K[0-9.]+' TestResults/CoverageReport/Summary.txt) |
| 66 | + echo "Current coverage: $COVERAGE%" |
| 67 | + THRESHOLD=70.0 |
| 68 | + if (( $(echo "$COVERAGE < $THRESHOLD" | bc -l) )); then |
| 69 | + echo "❌ Coverage ($COVERAGE%) is below threshold ($THRESHOLD%)" |
| 70 | + exit 1 |
| 71 | + else |
| 72 | + echo "✅ Coverage ($COVERAGE%) meets threshold ($THRESHOLD%)" |
| 73 | + fi |
0 commit comments