【问题标题】:Using the SonarQube tasks for Azure DevOps Server 2020 how can I suppress all the static code analysis, but upload the coverage report?使用 Azure DevOps Server 2020 的 SonarQube 任务如何抑制所有静态代码分析,但上传覆盖率报告?
【发布时间】:2026-01-20 17:55:01
【问题描述】:

我有一个构建:

  • 运行 dotCover 以生成覆盖率报告
  • 包括 SQ 准备和分析任务以将覆盖率报告上传到 SQ。

代码陈旧,存在大量静态代码分析问题。我决定通过在队列时间设置 SonarQubeExclude = true 构建变量来全局抑制 SQ 静态代码分析。

问题是SQ Analyze任务失败:

##[section]Starting: Run SQ Analysis
==============================================================================
Task         : Run Code Analysis
Description  : Run scanner and upload the results to the SonarQube server.
Version      : 4.18.0
Author       : sonarsource
Help         : Version: 4.18.0. This task is not needed for Maven and Gradle projects since the scanner should be run as part of the build.

[More Information](http://redirect.sonarsource.com/doc/install-configure-scanner-tfs-ts.html)
==============================================================================
[command]...
SonarScanner for MSBuild 5.1
Using the .NET Framework version of the Scanner for MSBuild
Post-processing started.
23:11:35.03  The exclude flag has been set so the project will not be analyzed. Project file: SomeProject.csproj
...
23:11:35.03  The exclude flag has been set so the project will not be analyzed. Project file: SomeProject2.csproj
##[error]23:11:35.046  No analysable projects were found. SonarQube analysis will not be performed. Check the build summary report for details.
23:11:35.046  No analysable projects were found. SonarQube analysis will not be performed. Check the build summary report for details.
23:11:35.03  The exclude flag has been set so the project will not be analyzed. Project file: SomeProject3.csproj
...
23:11:35.046  The exclude flag has been set so the project will not be analyzed. Project file: SomeProject4.csproj
23:11:35.046  Generation of the sonar-properties file failed. Unable to complete the analysis.
##[error]23:11:35.046  Post-processing failed. Exit code: 1
23:11:35.046  Post-processing failed. Exit code: 1
##[error]The process '...
##[section]Finishing: Run SQ Analysis

这会中止 SQ 分析任务,并且不会上传任何覆盖率报告。

我知道修复它的一种方法是创建一个没有静态代码分析规则的质量门,并配置相应的 SQ 项目以使用它。

我可以在不触及 SQ 服务器端配置的情况下使其工作吗?我不明白为什么 SonarQubeExclude 最后失败了 SQ 分析任务。

或多或少相同的 YAML 模板被许多构建重用了一些关闭 SQ 任务,一些打开它们(有条件的 YAML),但我不能做的是把 SQ Prepare 任务放在中间 - 所以要么它被打开在所有构建之前关闭或打开并运行。

SQ Prepare 任务:

  - task: SonarQubePrepare@4
    displayName: Prepare CI SQ Analysis
    inputs:
      SonarQube: SonarQube
      scannerMode: MSBuild
      projectKey: $(SonarQubeProjectKey)
      projectName: $(SonarQubeProjectName)
      projectVersion: $(SonarQubeProjectVersion)
      extraProperties: |
        sonar.cs.vscoveragexml.reportsPaths=$(Common.TestResultsDirectory)\vstest-coverage\*.xml
        sonar.cs.dotcover.reportsPaths=$(Common.TestResultsDirectory)\coverage\*.CoverageResult.html
        sonar.cs.opencover.reportsPaths=$(Common.TestResultsDirectory)\coverage\*.CoverageResult.xml
        sonar.cs.nunit.reportsPaths=$(Common.TestResultsDirectory)\tests\*.TestResult.xml
        sonar.inclusions=**/*.cs
        sonar.branch.name=$(SonarQubeSourceBranch)
        sonar.scm.disabled=true

SQ 分析任务:

      - task: SonarQubeAnalyze@4
        displayName: "Run SQ Analysis"

为了清楚起见,我省略了条件。

所有构建都发生在这两个任务之间。

是否可以完全禁止静态代码分析,只上传覆盖率报告而不更改服务器端设置?

【问题讨论】:

    标签: c# azure-devops msbuild sonarqube sonarqube-scan


    【解决方案1】:

    我不明白为什么 SonarQubeExclude 最后会导致 SQ 分析任务失败。

    设置该属性可以有效地告诉扫描程序,就好像该 MSBuild 项目不存在一样,因此不会报告任何问题或覆盖范围。 如果您已排除所有 MSBuild 项目,则扫描程序会抱怨没有要上传的内容。

    我可以在不触及 SQ 服务器端配置的情况下使其工作吗?

    没有支持的方式来做到这一点,不。支持的选项是上传问题和覆盖范围但只是不忽略问题,或者创建一个没有您描述的活动规则的质量配置文件。

    支持对旧代码进行分析的论点是检测安全漏洞,因此至少您知道它们。创建一个仅包含安全规则的有限质量配置文件并使用它可能是值得的。

    【讨论】:

    • 还有理由根本不对旧代码运行静态代码分析。这应该由用户自行决定。
    • 您为什么不想使用自定义质量配置文件来做到这一点?
    • 因为它使每个解决方案的控制变得更加困难。构建了几个解决方案。有些是旧的,有些是新的。对于旧的我想禁用静态代码分析,但对于新的我希望启用它。你建议怎么做?