3

我正在尝试将我的项目连接到 sonarcloud 中。我使用 Azure Devops Yaml 管道构建和启动我的测试。这是正在发生的事情的摘要。

首先我有一个准备分析任务:

  - task: SonarCloudPrepare@1
    displayName: 'Prepare analysis on Sonarcloud'
    inputs:
      SonarCloud: 'Sonarcloud'
      organization: ***redacted***
      scannerMode: 'MSBuild'
      projectKey: ***redacted***
      projectName: ***redacted***
      extraProperties: |
        sonar.exclusions=**/obj/**,**/*.dll
        sonar.branch.name=$(Build.SourceBranchName)
        sonar.cs.opencover.reportsPaths=$(Build.SourcesDirectory)/**/coverage.opencover.xml
        sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/*.trx

然后我建立项目,这里一切都很好。然后我启动测试,并使用报告生成器为我的 azure 管道创建一个 html 报告:

  - task: DotNetCoreCLI@2
    displayName: dotnet test
    inputs:
      command: test
      arguments: '--configuration $(BuildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=opencover --logger trx'
      projects: 'Tests/**/*.csproj'
      nobuild: true

  - script: |
      dotnet tool install -g dotnet-reportgenerator-globaltool
      reportgenerator "-reports:$(Build.SourcesDirectory)/**/coverage.opencover.xml" "-targetdir:$(Build.SourcesDirectory)/CodeCoverage" "-reporttypes:HtmlInline_AzurePipelines;Cobertura"
    displayName: Create Code coverage report

  - task: PublishCodeCoverageResults@1
    displayName: 'Publish code coverage'
    inputs:
      codeCoverageTool: Cobertura
      summaryFileLocation: '$(Build.SourcesDirectory)/CodeCoverage/Cobertura.xml'
      reportDirectory: '$(Build.SourcesDirectory)/CodeCoverage'

在这里一切都很好,测试运行,计算覆盖率,生成报告。

然后我安装 typescript(用于声纳分析)并运行代码分析:

  - script: |
      cd $(Build.SourcesDirectory)/Src/***ProjectDir***
      npm install typescript
    displayName: 'Install typescript for sonar analysis'
  - task: SonarCloudAnalyze@1
    displayName: 'Run code analysis'
    continueOnError: false
  - task: SonarCloudPublish@1
    displayName: 'Publish code analysis'
    continueOnError: false
    inputs:
      pollingTimeoutSec: '300'

即使在准备分析任务的 sonar.cs.opencover.reportsPaths 参数中提供了路径,声纳分析也会找到 trx 文件而不是 opencover 覆盖率报告。

这是日志:

D:\a\_tasks\SonarCloudPrepare_14d9cde6-c1da-4d55-aa01-2965cd301255\1.10.0\classic-sonar-scanner-msbuild\SonarScanner.MSBuild.exe end
SonarScanner for MSBuild 4.8
Using the .NET Framework version of the Scanner for MSBuild
Post-processing started.
17:31:43.118  Property 'sonar.cs.vstest.reportsPaths' provided, skipping the search for TRX files in default folders...
17:31:43.197  Did not find any binary coverage files in the expected location.
17:31:43.197  Falling back on locating coverage files in the agent temp directory.
17:31:43.197  Searching for coverage files in D:\a\_temp
17:31:43.243  No coverage files found in the agent temp directory.

分析已正确上传到 sonarcloud 但覆盖率显示为 0%...

4

1 回答 1

1

好吧,我的错误报道实际上是正确报告的。

罪魁祸首是 sonar.branch.name=$(Build.SourceBranchName) 我第一次运行构建时没有这个选项,并且覆盖不起作用,并且报告是完整的分支名称:文件夹/分支. 然后我将它与其他覆盖参数一起添加,并且该参数剥离了分支的文件夹,因此具有覆盖的新报告在分支中(没有文件夹)

然后我正在刷新当然没有更新的报告。

谢谢大家。

于 2020-03-31T09:23:34.717 回答