Search code examples
sonarqubegithub-actionssonarcloud

SonarScanner for .NET fails in GitHub Actions (.NET 5)


I'm trying to use SonarCloud in my GitHub Actions builds to analyze my code and to produce code coverage for my unit tests. I'm working on a .NET 5 solution in Visual Studio 2019.

At first, I was trying the SonarCloud GitHub action (https://github.com/SonarSource/sonarcloud-github-action), but the log told me to use the SonarScanner for .NET. I've setup my workflow file following the documentation: https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-msbuild/. I'm using the dotnet-sonarscanner global tool in the workflow.

The workflow looks like this:

jobs:
  BuildLinux:
    runs-on: ubuntu-latest

    steps:
      - name: 'Checkout Github Action'
        uses: actions/checkout@master
        with:
          # Disabling shallow clone is recommended for improving relevancy of reporting
          fetch-depth: 0
        
      - name: Setup dotnet
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: ${{ env.DOTNET_VERSION }}
     
      - name: Install Sonar global tool
        run: dotnet tool install --global dotnet-sonarscanner

      - name: Begin Sonar scan
        run: dotnet sonarscanner begin /k:"MySonarProject" /d:sonar.login=${{ secrets.SONAR_TOKEN }}

      - name: Build and run unit tests
        run: |
          cd src
          dotnet restore "MySolution.sln"
          cd MyAPI
          dotnet build --no-restore
          cd -
          cd MyApiUnitTests
          dotnet build --no-restore
          dotnet test --no-build --no-restore --verbosity normal -p:CollectCoverage=true -p:CoverletOutputFormat=opencover
          
      - name: End Sonar scan
        run: dotnet sonarscanner end /d:sonar.login=${{ secrets.SONAR_TOKEN }}

When I run this build, I get the following error on the 'Begin Sonar scan' step. The installation of the global tool has succeeded.

SonarScanner for MSBuild 5.2.1
Using the .NET Core version of the Scanner for MSBuild
Pre-processing started.
Preparing working directories...
05:54:44.604  Updating build integration targets...
05:54:44.688  Failed to request and parse 'http://localhost:9000/api/server/version': Connection refused (localhost:9000)
Unhandled exception. System.Net.Http.HttpRequestException: Connection refused (localhost:9000)
 ---> System.Net.Sockets.SocketException (111): Connection refused
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
   at System.Net.Sockets.Socket.<ConnectAsync>g__WaitForConnectWithCancellation|283_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.DefaultConnectAsync(SocketsHttpConnectionContext context, CancellationToken cancellationToken)
   at System.Net.Http.ConnectHelper.ConnectAsync(Func`3 callback, DnsEndPoint endPoint, HttpRequestMessage requestMessage, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.ConnectHelper.ConnectAsync(Func`3 callback, DnsEndPoint endPoint, HttpRequestMessage requestMessage, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.SendAsyncCore(HttpRequestMessage request, HttpCompletionOption completionOption, Boolean async, Boolean emitTelemetryStartStop, CancellationToken cancellationToken)
   at SonarScanner.MSBuild.PreProcessor.WebClientDownloader.Download(String url, Boolean logPermissionDenied)
   at SonarScanner.MSBuild.PreProcessor.SonarWebService.<>c__DisplayClass18_0.<<DownloadServerVersion>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at SonarScanner.MSBuild.PreProcessor.SonarWebService.DoLogExceptions[T](Func`1 op, String url)
   at SonarScanner.MSBuild.PreProcessor.SonarWebService.DownloadServerVersion()
   at SonarScanner.MSBuild.PreProcessor.SonarWebService.GetServerVersion()
   at SonarScanner.MSBuild.PreProcessor.SonarWebService.WarnIfSonarQubeVersionIsDeprecated()
   at SonarScanner.MSBuild.PreProcessor.TeamBuildPreProcessor.DoExecute(ProcessedArgs localSettings)
   at SonarScanner.MSBuild.PreProcessor.TeamBuildPreProcessor.Execute(String[] args)
   at SonarScanner.MSBuild.BootstrapperClass.PreProcess()
   at SonarScanner.MSBuild.BootstrapperClass.Execute()
   at SonarScanner.MSBuild.Program.Execute(String[] args, ILogger logger)
   at SonarScanner.MSBuild.Program.Execute(String[] args)
   at SonarScanner.MSBuild.Program.Main(String[] args)
   at SonarScanner.MSBuild.Program.<Main>(String[] args)

The build runs on ubuntu-latest, I also tried windows-latest. Same result.

How can I solve this issue? Can I use the Sonar scanner like this in GitHub Actions in the first place?


Solution

  • The log shows that it tries to connect to local instance of SonarQube, not SonarCloud. You would need to configure additional parameters to connect to SonarCloud: /d:sonar.host.url=https://sonarcloud.io and /o:<your organization>. See SonarCloud version of task documentation and SonarCloud required parameters.