Using GitHub Actions to analyse a .NET Core app built on Ubuntu

I am trying to set up GitHub Actions to use SonarQube Community edition for a .NET Core app that needs to be built on Linux rather than Windows; however, the documentation assumes you are building on Windows.

  • SonarQube Community Build v24.12.0.100206

My GitHub Actions workflow looks something like this:

  build:
    name: Build and test
    runs-on: ubuntu-latest
    needs: [ version ]
    steps:

    - name: Checkout source
      uses: actions/checkout@v4
      with:
        fetch-depth: 0

    - name: Set up .NET Core
      uses: actions/setup-dotnet@v4
      with:
        dotnet-version: 8.0.x

    - name: Set up JDK 17
      uses: actions/setup-java@v4
      with:
        java-version: 17
        distribution: 'zulu'

    - name: Cache SonarQube scanner
      id: cache-sonar-scanner
      uses: actions/cache@v4
      with:
        path: .\.sonar\scanner
        key: ${{ runner.os }}-sonar-scanner
        restore-keys: ${{ runner.os }}-sonar-scanner

    - name: Install SonarQube scanner
      if: steps.cache-sonar-scanner.outputs.cache-hit != 'true'
      shell: pwsh
      run: |
        New-Item -Path .\.sonar\scanner -ItemType Directory
        dotnet tool update dotnet-sonarscanner --tool-path .\.sonar\scanner

    - name: Install DotNet-Coverage tool
      run: dotnet tool install --global dotnet-coverage

    - name: Set NuGet credentials
      run: dotnet nuget update source github --username USERNAME --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text

    - name: Build and analyze
      shell: powershell
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}  # Needed to get PR information, if any
        SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
      run: |
        .\.sonar\scanner\dotnet-sonarscanner begin /k:"${{ env.SONAR_PROJECT_ID }}" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="${{ vars.SONAR_HOST_URL }}" /d:sonar.cs.vscoveragexml.reportsPaths="${{ env.TEST_OUTPUT }}/${{ env.TEST_FILE }}"
        dotnet-coverage collect "dotnet test ${{ env.BUILD_SOLUTION }} /p:Version=${{ needs.version.outputs.GitFullSemVer }} --configuration ${{ env.BUILD_CONFIGURATION }} --settings ""CodeCoverage.runsettings"" --filter ""${{ env.TEST_FILTER }}""" -f xml -o "${{ env.TEST_OUTPUT }}/${{ env.TEST_FILE }}"
        .\.sonar\scanner\dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"

However, the build process fails with the following error:

.\.sonar\scanner\dotnet-sonarscanner: /home/runner/work/_temp/1942123b-5342-49d9-a722-dcd65f2bb797.ps1:2
Line |
   2 |  .\.sonar\scanner\dotnet-sonarscanner begin /k:"my-project" /d:sonar.toke …
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | The term '.\.sonar\scanner\dotnet-sonarscanner' is not recognized as a
     | name of a cmdlet, function, script file, or executable program. Check
     | the spelling of the name, or if a path was included, verify that the
     | path is correct and try again.
Error: Process completed with exit code 1.

If I change the job to use ubuntu-latest instead of windows-latest then the process completes, although my tests fail because they require Linux rather than Windows.

I’m wondering if it’s something to do with Linux paths vs Windows paths?

I noticed Can we use ubuntu runner for .net projects in github action - SonarQube Server / Community Build - Sonar Community asks a similar question, but the issue remains unresolved.

I switched to Linux-format on the path properties (e.g. path: ./sonar/scanner) and now things are running fine with pwsh on ubuntu-latest

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.