You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure DevOps流水线Sonar-scanner执行项目构建器后失败

问题场景
  • 环境:Azure DevOps流水线运行在Kubernetes部署的自托管代理上,使用Azure应用市场的SonarQube扩展,流水线执行顺序为先运行Prepare Analysis Configuration任务,紧接Run Code Analysis任务执行代码质量扫描。
  • 现象:完成SonarQube与Azure DevOps的对接配置后,流水线运行Run Code Analysis任务时报错,核心错误提示为使用sonar.branch.name属性开展分支分析需要SonarQube Developer Edition及以上版本,扫描进程最终以退出码2终止,错误发生在Execute project builders阶段完成后。
  • 已知信息:确认SonarQube社区版与付费版本的分支分析功能存在差异,但通过exec进入自托管Azure代理节点,手动执行同路径下的sonar-scanner命令(指定sonar.host.url、sonar.login、sonar.projectKey参数并追加-Dsonar.scm.disabled=true)时,扫描可正常执行成功。
流水线报错日志
INFO: SonarScanner 4.6.2.2472
INFO: Java 11.0.15 Private Build (64-bit)
INFO: Linux 5.4.17-2136.304.4.1.el7uek.x86_64 amd64
INFO: User cache: /root/.sonar/cache
INFO: Scanner configuration file: /azp/_work/_tasks/SonarQubeAnalyze_6d01813a-9589-4b15-8491-8164aeb38055/5.6.1/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: /azp/_work/1/s/sonar-project.properties
INFO: Analyzing on SonarQube server 9.4.0
INFO: Default locale: "en_US", source code encoding: "UTF-8" (analysis is platform dependent)
INFO: Load global settings
INFO: Load global settings (done) | time=112ms
INFO: Server id: 41D5D374-AYHzDcd0-Nvlhdy2JQfz
INFO: User cache: /root/.sonar/cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=40ms
INFO: Load/download plugins (done) | time=143ms
INFO: Process project properties
INFO: Process project properties (done) | time=1ms
INFO: Execute project builders
INFO: Execute project builders (done) | time=3ms
WARN: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ClassRealm{javascript}-org.sonar.plugins.javascript.eslint.EslintBridgeServerImpl': Unsatisfied dependency expressed through constructor parameter 4; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'AnalysisTempFolder' defined in org.sonar.scanner.analysis.AnalysisTempFolderProvider: Unsatisfied dependency expressed through method 'provide' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'DefaultInputProject' defined in org.sonar.scanner.scan.InputProjectProvider: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.sonar.api.batch.fs.internal.DefaultInputProject]: Factory method 'provide' threw exception; nested exception is Validation of project failed:
  o To use the property "sonar.branch.name" and analyze branches, Developer Edition or above is required. See https://redirect.sonarsource.com/doc/branches.html for more information.
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 1.808s
INFO: Final Memory: 16M/237M
INFO: ------------------------------------------------------------------------
##[error]ERROR: Error during SonarScanner execution
ERROR: Validation of project failed:
  o To use the property "sonar.branch.name" and analyze branches, Developer Edition or above is required. See https://redirect.sonarsource.com/doc/branches.html for more information.
ERROR: Error during SonarScanner execution
ERROR: Validation of project failed:
##[error]ERROR: 
ERROR: Re-run SonarScanner using the -X switch to enable full debug logging.

ERROR: 
ERROR: Re-run SonarScanner using the -X switch to enable full debug logging.
##[error]The process '/azp/_work/_tasks/SonarQubeAnalyze_6d01813a-9589-4b15-8491-8164aeb38055/5.6.1/sonar-scanner/bin/sonar-scanner' failed with exit code 2
Finishing: Run Code Analysis
手动执行验证信息

手动执行命令:

/azp/_work/_tasks/SonarQubeAnalyze_6d01813a-9589-4b15-8491-8164aeb38055/5.6.1/sonar-scanner/bin/sonar-scanner -Dsonar.host.url=<our-server-url> -Dsonar.login=<sonarqube-generated-token> -Dsonar.projectKey=<the-project-key> -Dsonar.scm.disabled=true

手动执行成功日志片段:

INFO: Scanner configuration file: /azp/_work/_tasks/SonarQubeAnalyze_6d01813a-9589-4b15-8491-8164aeb38055/5.6.1/sonar-scanner/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarScanner 4.6.2.2472
INFO: Java 11.0.15 Private Build (64-bit)
INFO: Linux 5.4.17-2136.304.4.1.el7uek.x86_64 amd64
INFO: User cache: /root/.sonar/cache
INFO: Scanner configuration file: /azp/_work/_tasks/SonarQubeAnalyze_6d01813a-9589-4b15-8491-8164aeb38055/5.6.1/sonar-scanner/conf/sonar-scanner.properties
...
INFO: Analysis total time: 1:11.998 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 1:13.344s
INFO: Final Memory: 46M/237M
INFO: ------------------------------------------------------------------------

根因分析

流水线执行失败和手动执行成功的核心差异是参数注入逻辑不同:

  1. Azure DevOps的SonarQube扩展中,Prepare Analysis Configuration任务默认会自动读取当前流水线运行的分支名,主动注入sonar.branch.name参数传给后续扫描任务,SonarQube社区版不支持该分支分析参数,直接触发校验失败。
  2. 流水线执行时扫描器会自动加载代码目录下的sonar-project.properties文件,如果文件内硬编码了sonar.branch.name配置也会触发报错;手动执行时未加载到该配置文件,同时追加了sonar.scm.disabled=true禁用SCM分支自动探测逻辑,不会携带分支相关参数,因此扫描正常通过。
解决方案

按优先级选择对应方案即可修复:

  • 方案1(适配社区版,无额外成本):编辑流水线中的Prepare Analysis Configuration任务,在「额外属性」配置段追加以下两行配置,覆盖自动注入的分支参数,和手动执行的生效参数对齐:
sonar.branch.name=
sonar.scm.disabled=true
  • 方案2:检查代码仓库根目录的sonar-project.properties文件,若存在sonar.branch.name配置项直接删除,避免扫描时读取到该无效参数。
  • 方案3:如果需要使用官方分支扫描、PR质量门禁等能力,将SonarQube实例升级到Developer Edition及以上付费版本即可。

内容的提问来源于stack exchange,提问作者Paula Gouveia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 12:45:42