我在 Ubuntu 计算机上使用社区版 SonarQube,并在 Azure DevOps 中使用自托管代理。 我尝试使用两种不同的代码将 SonarQube 集成到管道中,但似乎都不起作用。
第一条管道
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'
但这是我收到的错误
##[警告]此版本中未找到任何分析!请检查您的构建配置。
第二条管道
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'
我最终收到了
- 警告:此任务配置为使用 Java 11,但 SonarQube 服务器 v10.5.0 不支持它。忽略配置并使用 JAVA_HOME 代替。在任务定义中指定 jdkversion 以使用 Java 17 删除此警告。**
- 错误:未授权。请检查属性“sonar.token”中的用户令牌或属性“sonar.login”和“sonar.password”中的凭据。
尽管我已经验证了 SonarQube 的权限和令牌,并在
sonarqube.properties
中添加了我的凭据。但是,Gradle 创建的仅包含 Default SonarQube server
和 Default source code encoding
对于您的第一个管道,我注意到您昨天问了类似的问题,并发现您的 Gradle@3 任务失败了。我相信这次失败的测试导致无法生成 SonarQube 所需的文件。您需要修复错误并使构建成功。
对于您的第二个管道,在SonarQube的Azure DevOps集成文档中,它提到运行代码分析任务未在Maven或Gradle项目中使用。所以这个任务是不需要的。它提供了为 Gradle 构建 Azure 管道的步骤,它使用
scannerMode: 'Other'
,这与您的第一个管道相同。