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

SonarQube与Azure DevOps集成失败求助:两种Pipeline均报错

问题:Ubuntu环境下Azure DevOps自托管代理集成SonarQube社区版失败

第一种Pipeline配置及执行警告

steps:
- task: SonarQubePrepare@5
  inputs:
    SonarQube: 'name_of_service_connection'
    scannerMode: 'Other'
    extraProperties: |
      # Additional properties that will be passed to the scanner,
      # Put one key=value per line, example:
      # sonar.exclusions=**/*.bin
      sonar.projectKey=my_sonar_project_key
      sonar.projectName=my_project_name

- script: |
    java -version
    wget https://builds.openlogic.com/downloadJDK/openlogic-openjdk/17.0.10+7/openlogic-openjdk-17.0.10+7-linux-x64.tar.gz -O JAVA_HOME_17_X64.tar.gz
    tree
  displayName: Download JDK17

- task: JavaToolInstaller@0
  inputs:
    versionSpec: '17'
    jdkArchitectureOption: 'x64'
    jdkSourceOption: 'LocalDirectory'
    jdkFile: 'JAVA_HOME_17_X64.tar.gz'
    jdkDestinationDirectory: '$(Agent.ToolsDirectory)/JAVA_HOME_17/X64'
    cleanDestinationDirectory: true
    createExtractDirectory: false


- script: |
    java -version
    echo Environment Variable JAVA_HOME_17_X64 is $(JAVA_HOME_17_X64)
    tree $(Agent.ToolsDirectory)
  displayName: Check JDK version

- task: Gradle@3
  inputs:
    gradleWrapperFile: '$(Build.Repository.LocalPath)/gradlew'
    workingDirectory: '$(Build.Repository.LocalPath)'
    tasks: 'build'
    publishJUnitResults: true
    testResultsFiles: '**/TEST-*.xml'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: '1.17' # Use pre-installed JAVA17
    gradleOptions: '-Xmx3072m'
    sonarQubeRunAnalysis: true
    sqGradlePluginVersionChoice: 'specify'
    sonarQubeGradlePluginVersion: '5.0.0.4638'
    spotBugsAnalysis: false
    
- task: SonarQubePublish@5
  condition: always()
  inputs:
    pollingTimeoutSec: '300'

执行后警告

##[warning]No analyses found in this build! Please check your build configuration.


第二种Pipeline配置及执行问题

steps:
- script: |
    java -version
    wget https://builds.openlogic.com/downloadJDK/openlogic-openjdk/17.0.10+7/openlogic-openjdk-17.0.10+7-linux-x64.tar.gz -O JAVA_HOME_17_X64.tar.gz
    tree
  displayName: Download JDK17

- task: SonarQubePrepare@5
  displayName: Prepare SonarQube
  condition: always()
  inputs:
    SonarQube: 'name_of_service_connection'
    scannerMode: 'CLI'
    configMode: 'file'
    extraProperties: |
      sonar.projectKey= my_sonar_project_key
      sonar.projectName=$(SONAR_PREFIX_KEY)-$(app-name):$(app-name)
      sonar.branch.name='$(SonarBranchName)'
      sonar.host.url= my_sonar_url
      sonar.login= my_sonar_token

- task: JavaToolInstaller@0
  inputs:
    versionSpec: '17'
    jdkArchitectureOption: 'x64'
    jdkSourceOption: 'LocalDirectory'
    jdkFile: 'JAVA_HOME_17_X64.tar.gz'
    jdkDestinationDirectory: '$(Agent.ToolsDirectory)/JAVA_HOME_17/X64'
    cleanDestinationDirectory: true
    createExtractDirectory: false

- script: |
    java -version
    echo Environment Variable JAVA_HOME_17_X64 is $(JAVA_HOME_17_X64)
    tree $(Agent.ToolsDirectory)
  displayName: Check JDK version

- task: Gradle@3
  inputs:
    gradleWrapperFile: '$(Build.Repository.LocalPath)/gradlew'
    workingDirectory: '$(Build.Repository.LocalPath)'
    tasks: 'build'
    publishJUnitResults: true
    testResultsFiles: '**/TEST-*.xml'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: '1.17' # Use pre-installed JAVA17
    gradleOptions: '-Xmx3072m'
    sonarQubeRunAnalysis: true
    sqGradlePluginVersionChoice: 'specify'
    sonarQubeGradlePluginVersion: '5.0.0.4638'
    spotBugsAnalysis: false


- task: SonarQubeAnalyze@5
  condition: always()
  displayName: Run SonarQube analysis

- task: SonarQubePublish@5
  displayName: 'Publish SonarQube analysis'
  condition: always()
  inputs:
    pollingTimeoutSec: '300'

- task: sonar-buildbreaker@8
  condition: and(succeeded(),eq(variables.ableToScan, 'true'))
  displayName: "Evaluate SonarQube results"
  inputs:
    SonarQube: 'name_of_service_connection'

执行后问题

  • warning : This task was configured to use Java 11, but the SonarQube server v10.5.0 does not support it. Ignoring the configuration and using JAVA_HOME instead. Specify jdkversion in your task definition to use Java 17 to remove this warning.
  • error : Not authorized. Please check the user token in the property 'sonar.token' or the credentials in the properties 'sonar.login' and 'sonar.password'.

附加信息

已验证SonarQube权限和令牌,且在sonarqube.properties中配置了凭据,但Gradle生成的文件仅包含Default SonarQube server和Default source code encoding。


针对性解决方案

第一种Pipeline「No analyses found」问题修复

  1. 调整任务顺序:将SonarQubePrepare@5移到JavaToolInstaller@0之后执行,确保SonarQube任务能获取到正确的JDK17环境。
  2. 修改Gradle任务触发参数:把Gradle任务的tasks字段从build改为build sonarqube,让Gradle在构建后自动触发Sonar扫描。
  3. 移除冗余任务:使用Gradle插件执行扫描时,SonarQubePublish@5会与Gradle扫描流程冲突,直接移除该任务。

第二种Pipeline问题修复

1. Java版本警告消除

在所有SonarQube相关任务(SonarQubePrepare@5、SonarQubeAnalyze@5、SonarQubePublish@5、sonar-buildbreaker@8)中添加JDK版本指定配置,示例:

- task: SonarQubeAnalyze@5
  condition: always()
  displayName: Run SonarQube analysis
  inputs:
    jdkVersionOption: '1.17'

2. 权限验证失败修复

  • 清理配置项空格:sonar.projectKey= my_sonar_project_key这类配置的等号后存在空格,会导致参数解析失败,改为sonar.projectKey=my_sonar_project_key格式。
  • 复用服务连接配置:既然已配置SonarQube服务连接name_of_service_connection,无需在extraProperties中手动指定sonar.host.url和sonar.login,服务连接会自动注入这些参数,避免冲突。
  • 验证令牌权限:确认SonarQube令牌拥有目标项目的「分析执行」和「质量门查看」权限,且令牌未过期。

通用优化建议

  1. 统一JDK安装时机:将JavaToolInstaller@0放到Pipeline最开头,确保所有后续任务都使用JDK17。
  2. 适配插件版本:SonarQube Server 10.5.0建议搭配5.1.x版本的SonarQube Gradle插件,可在项目build.gradle中直接声明插件依赖,避免Pipeline重复配置。
  3. 排查Gradle配置:执行./gradlew sonarqube -Dsonar.verbose=true查看详细日志,确认SonarQube参数是否被正确注入。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 14:05:14