.NET 的 SonarScanner 在 GitHub 操作中失败 (.NET 5)

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

我正在尝试在我的 GitHub Actions 构建中使用 SonarCloud 来分析我的代码并为我的单元测试生成代码覆盖率。我正在 Visual Studio 2019 年开发 .NET 5 解决方案。

起初,我正在尝试 SonarCloud GitHub 操作 (https://github.com/SonarSource/sonarcloud-github-action),但日志告诉我使用 SonarScanner for .NET。 我已经按照文档设置了我的工作流文件:https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-msbuild/。 我在工作流中使用了 dotnet-sonarscanner 全局工具。

工作流程如下所示:

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 }}

当我 运行 此构建时,我在 'Begin Sonar scan' 步骤中收到以下错误。全局工具安装成功

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)

构建 运行s 在 ubuntu-latest,我也试过 windows-latest。同样的结果。

我该如何解决这个问题?我可以首先在 GitHub 操作中使用像这样的声纳扫描仪吗?

日志显示它尝试连接到 SonarQube 的本地实例,而不是 SonarCloud。您需要配置其他参数才能连接到 SonarCloud:/d:sonar.host.url=https://sonarcloud.io/o:<your organization>。参见 SonarCloud version of task documentation and SonarCloud required parameters