|
| 1 | +# SonarCloud analysis with test coverage (CI-based scanner). |
| 2 | +# Requires a GitHub org-level secret SONAR_TOKEN, and Automatic Analysis turned OFF for |
| 3 | +# project PFalkowski_LoggerLite (SonarCloud > Administration > Analysis Method). |
| 4 | +name: SonarCloud |
| 5 | + |
| 6 | +on: |
| 7 | + push: |
| 8 | + branches: [ master ] |
| 9 | + pull_request: |
| 10 | + branches: [ master ] |
| 11 | + |
| 12 | +jobs: |
| 13 | + sonar: |
| 14 | + name: Build, test & analyze |
| 15 | + runs-on: ubuntu-latest |
| 16 | + # Skip PRs from forks — they can't access the SONAR_TOKEN secret. |
| 17 | + if: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository }} |
| 18 | + steps: |
| 19 | + - name: Checkout |
| 20 | + uses: actions/checkout@v6 |
| 21 | + with: |
| 22 | + fetch-depth: 0 |
| 23 | + |
| 24 | + - name: Set up JDK (required by the scanner) |
| 25 | + uses: actions/setup-java@v4 |
| 26 | + with: |
| 27 | + distribution: temurin |
| 28 | + java-version: 17 |
| 29 | + |
| 30 | + - name: Setup .NET |
| 31 | + uses: actions/setup-dotnet@v5 |
| 32 | + with: |
| 33 | + dotnet-version: 8.0.x |
| 34 | + |
| 35 | + - name: Cache SonarCloud packages |
| 36 | + uses: actions/cache@v4 |
| 37 | + with: |
| 38 | + path: ~/.sonar/cache |
| 39 | + key: ${{ runner.os }}-sonar |
| 40 | + restore-keys: ${{ runner.os }}-sonar |
| 41 | + |
| 42 | + - name: Install scanner + coverage tools |
| 43 | + run: | |
| 44 | + dotnet tool install --global dotnet-sonarscanner |
| 45 | + dotnet tool install --global dotnet-coverage |
| 46 | +
|
| 47 | + - name: Build, test (with coverage) & analyze |
| 48 | + env: |
| 49 | + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
| 50 | + run: | |
| 51 | + dotnet-sonarscanner begin \ |
| 52 | + /k:"PFalkowski_LoggerLite" /o:"pfalkowski" \ |
| 53 | + /d:sonar.host.url="https://sonarcloud.io" \ |
| 54 | + /d:sonar.token="$SONAR_TOKEN" \ |
| 55 | + /d:sonar.exclusions="**/*TestHarness/**" \ |
| 56 | + /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml |
| 57 | + dotnet build --configuration Release |
| 58 | + dotnet-coverage collect "dotnet test --configuration Release --no-build" -f xml -o coverage.xml |
| 59 | + dotnet-sonarscanner end /d:sonar.token="$SONAR_TOKEN" |
0 commit comments