当由 jenkins 管道执行时,sonar-scanner.bat 在 api/settings/values.protobuf 上失败并显示 401

sonar-scanner.bat fails with 401 on api/settings/values.protobuf when executed by jenkins pipeline

当我执行下面的管道时,我在 http://mysonarhost.my.domain:9000/api/settings/values.protobuf 上收到 401 错误。

同一命令在命令提示符下执行时没有错误。 api/settings/values.protobuf URL 可以使用浏览器访问。

我尝试在不同的地方设置令牌,但 none 似乎有帮助。

版本

流水线

https://i.imgur.com/S3Vk0fl.png

日志

sonar-scanner -X -Dproject.settings=..\sonar_ccesmarttools.properties -Dsonar.logon=mytoken 
06:32:33.829 INFO: Scanner configuration file: X:\workspaces\common\tools\hudson.plugins.sonar.SonarRunnerInstallation\SonarQubeScanner\bin\..\conf\sonar-scanner.properties
06:32:33.836 INFO: Project root configuration file: X:\cce\SmartLisaNightly\git\ccesmarttools\CceSmartTools\..\sonar_ccesmarttools.properties
06:32:33.873 INFO: SonarQube Scanner 3.0.3.778
06:32:33.874 INFO: Java 1.8.0_73 Oracle Corporation (64-bit)
06:32:33.874 INFO: Windows Server 2012 R2 6.3 amd64
06:32:33.874 INFO: SONAR_SCANNER_OPTS=-Xmx16g
06:32:34.041 DEBUG: keyStore is : 
06:32:34.042 DEBUG: keyStore type is : jks
06:32:34.042 DEBUG: keyStore provider is : 
06:32:34.042 DEBUG: init keystore
06:32:34.042 DEBUG: init keymanager of type SunX509
06:32:34.158 INFO: User cache: C:\Users\jenkinsuser\.sonar\cache
06:32:34.158 DEBUG: Extract sonar-scanner-api-batch in temp...
06:32:34.173 DEBUG: Get bootstrap index...
06:32:34.173 DEBUG: Download: http://mysonarhost.my.domain:9000/batch/index
06:32:34.232 DEBUG: Get bootstrap completed
06:32:34.233 DEBUG: Create isolated classloader...
06:32:34.243 DEBUG: Start temp cleaning...
06:32:34.255 DEBUG: Temp cleaning done
06:32:34.255 DEBUG: Execution getVersion
06:32:34.260 DEBUG: Execution start
06:32:34.557 DEBUG: Publish global mode
06:32:34.698 INFO: Load global settings
06:32:34.749 DEBUG: GET 401 http://mysonarhost.my.domain:9000/api/settings/values.protobuf | time=43ms
06:32:34.751 INFO: ------------------------------------------------------------------------
06:32:34.751 INFO: EXECUTION FAILURE
06:32:34.751 INFO: ------------------------------------------------------------------------
06:32:34.751 INFO: Total time: 0.961s
06:32:34.788 INFO: Final Memory: 17M/1963M
06:32:34.788 INFO: ------------------------------------------------------------------------
06:32:34.788 ERROR: Error during SonarQube Scanner execution
java.lang.IllegalStateException: Unable to load component class org.sonar.scanner.bootstrap.ScannerPluginInstaller

根据 documentation,属性 应该是 sonar.login,而不是 sonar.logon

感谢 Gilles QUERRET 的回答和评论,我设法解决了这个问题。

  • 令牌应该在 sonar.login 属性
  • 添加了 withCredentials 节点
  • http://myjenkinsserver:8090/credentials/store/system/domain/_/
  • 中存储了 ID 为 SONAR_TOKEN 的令牌
  • 将 bat 命令中的单引号更改为双引号以允许计算 $SONAR_TOKEN
  • 不是初始问题的一部分,已添加 -Dsonar.projectVersion=${env.BUILD_NUMBER} 以在 SonarQube 中显示 Jenkins 内部版本号

工作阶段定义

stage('SonarQube analysis') {
  withSonarQubeEnv('SonarQube') {
      withCredentials([string(credentialsId: 'SONAR_TOKEN', variable: 'SONAR_TOKEN')]) {
          def scannerHome = tool 'SonarQubeScanner';
          env.PATH = "${scannerHome}\bin;${env.PATH}";
          dir('X:\cce\SmartLisaNightly\git\smartlisa\SmartLisaFrontend') {
              bat "sonar-scanner -Dsonar.login=$SONAR_TOKEN -Dproject.settings=..\sonar_SmartLisaFrontend.properties -Dsonar.projectVersion=${env.BUILD_NUMBER}"
          }
      }
  }
}