.NET App Coverage Integration on GitHub Action

Hello Team;

I need your support about .Net repository SonarCloud coverage integration step. I have GitHub repository already linked SonarCloud. I’m trying modify my GitHub action workflow file for coverage function. But i receive below error .

/home/runner/actions-runner/_work/_temp/d22f2b8c–b86d-cfd062170f67.sh: line 3: dotnet-coverage: command not found
Error: Process completed with exit code 127.

Here is my part of workflow file. Can you help me resolve the issue please?

   - name: Install SonarCloud scanner
        run: |
         mkdir -p .sonar/scanner
         dotnet tool update dotnet-sonarscanner --tool-path ./.sonar/scanner
         dotnet tool install --global dotnet-sonarscanner
         chmod +x ./.sonar/scanner/dotnet-sonarscanner

      - name: Install dotnet-coverage globally
        run: | 
         dotnet tool install -g dotnet-coverage

      - name: Build and analyze API
        env:
         GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
         SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        run: |
         ./.sonar/scanner/dotnet-sonarscanner begin /k:"X" /o:"Y" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.opencover.reportsPaths="./coverage.opencover"
          dotnet build --no-incremental
          dotnet-coverage collect 'dotnet test' -f xml  -o 'coverage.xml' test 
          dotnet-coverage report --reports "./coverage.opencover" -f lcov -o "./coverage.lcov"
         ./.sonar/scanner/dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"

Hey there.

In your action, what do the logs look like for the step? Was the install successful? Take notice of the other steps that had to occur for you to use dotnet-sonarscanner (and you had to specify the path to the tool when actually executing it)

Hello Colin;

You can see the workflow configuration and result below. Coverage it is look like %0.

name: SonarCloud Scan

on:
  push:
    branches:
      - main

jobs:
  sonar_scan:
    runs-on: xyz

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Use .NET 8
        uses: actions/setup-dotnet@v2
        with:
          dotnet-version: '8.0.x'

      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          distribution: 'temurin'
          java-version: '17'
          overwrite-settings: false

      - name: Install SonarCloud scanner
        run: |
          mkdir -p .sonar/scanner
          dotnet tool update dotnet-sonarscanner --tool-path ./.sonar/scanner
          dotnet tool install --global dotnet-sonarscanner
          chmod +x ./.sonar/scanner/dotnet-sonarscanner
      - name: Install dotnet-coverage globally
        run: | 
          dotnet tool install -g dotnet-coverage
      - name: Sonar Scan
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        run: |
          ./.sonar/scanner/dotnet-sonarscanner begin /k:"xyz" /o:"tyz" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.opencover.reportsPaths="./coverage.opencover"
          dotnet buildx.sln --no-incremental
          x.sln --no-build --logger "trx;LogFileName=test-results.trx"
          ./.sonar/scanner/dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"

Well, now you simply aren’t running dotnet-coverage at all.

I would really suggest you focus on generating a valid coverage report (in a format that SonarCloud accepts), and then worry about whether or not the results show up on SonarCloud. SonarCloud is only involved in reading the report, not generating it.