我在SonarQube质量门上有这个错误:
org.sonarqube.ws.client.HttpException:错误401
我在Jenkins的第三阶段构建管道过程中遇到上述错误。
SonarQube扫描仪版本:4.5.0.2216
SonarQube服务器版本:8.6.0.39681
Jenkins服务器版本:2.235.5
下面是我的构建管道
node {
stage('Clone the Git') {
git 'https://github.com/SonarSource/sonar-scanning-examples'
}
stage('SonarQube analysis') {
def scannerHome = tool 'sonarqube';
withSonarQubeEnv('sonarqube-server') {
sh "${scannerHome}/bin/sonar-scanner \
-D sonar.login=admin \
-D sonar.password=admin \
-D sonar.projectKey=sonarqubetest3 \
-D sonar.exclusions=vendor/**,resources/**,**/*.java \
-D sonar.host.url=http://192.168.0.4:9000/"
}
}
stage('Quality Gates'){
timeout(time: 1, unit: 'HOURS') {
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
}
以下是完整的Jenkins作业日志:
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/sonar-qube-testing
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Clone the Git)
[Pipeline] git
The recommended git tool is: NONE
No credentials specified
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://github.com/SonarSource/sonar-scanning-examples # timeout=10
Fetching upstream changes from https://github.com/SonarSource/sonar-scanning-examples
> git --version # timeout=10
> git --version # 'git version 2.11.0'
> git fetch --tags --progress -- https://github.com/SonarSource/sonar-scanning-examples +refs/heads/*:refs/remotes/origin/* # timeout=10
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 1cbdd40467f475fcdbd8367ca00b65acbf774e3f (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f 1cbdd40467f475fcdbd8367ca00b65acbf774e3f # timeout=10
> git branch -a -v --no-abbrev # timeout=10
> git branch -D master # timeout=10
> git checkout -b master 1cbdd40467f475fcdbd8367ca00b65acbf774e3f # timeout=10
Commit message: "Merge pull request #99 from SonarSource/pre-8.x-hardening"
> git rev-list --no-walk 1cbdd40467f475fcdbd8367ca00b65acbf774e3f # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (SonarQube analysis)
[Pipeline] tool
[Pipeline] withSonarQubeEnv
Injecting SonarQube environment variables using the configuration: sonarqube-server
[Pipeline] {
[Pipeline] sh
+ /var/jenkins_home/tools/hudson.plugins.sonar.SonarRunnerInstallation/sonarqube/bin/sonar-scanner -D sonar.login=admin -D sonar.password=admin -D sonar.projectKey=sonarqubetest3 -D sonar.exclusions=vendor/**,resources/**,**/*.java -D sonar.host.url=http://192.168.0.4:9000
INFO: Scanner configuration file: /var/jenkins_home/tools/hudson.plugins.sonar.SonarRunnerInstallation/sonarqube/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarScanner 4.5.0.2216
INFO: Java 1.8.0_242 Oracle Corporation (64-bit)
INFO: Linux 4.15.0-111-generic amd64
INFO: User cache: /var/jenkins_home/.sonar/cache
INFO: Scanner configuration file: /var/jenkins_home/tools/hudson.plugins.sonar.SonarRunnerInstallation/sonarqube/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: Analyzing on SonarQube server 8.6.0
INFO: Default locale: "en", source code encoding: "UTF-8" (analysis is platform dependent)
WARN: SonarScanner will require Java 11 to run starting in SonarQube 8.x
INFO: Load global settings
INFO: Load global settings (done) | time=658ms
INFO: Server id: BF41A1F2-AXaTGa1I2xlvXb1FgNjK
INFO: User cache: /var/jenkins_home/.sonar/cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=602ms
INFO: Load/download plugins (done) | time=649ms
INFO: Process project properties
INFO: Process project properties (done) | time=1ms
INFO: Execute project builders
INFO: Execute project builders (done) | time=2ms
INFO: Project key: sonarqubetest3
INFO: Base dir: /var/jenkins_home/workspace/sonar-qube-testing
INFO: Working dir: /var/jenkins_home/workspace/sonar-qube-testing/.scannerwork
INFO: Load project settings for component key: 'sonarqubetest3'
INFO: Load project settings for component key: 'sonarqubetest3' (done) | time=574ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=636ms
INFO: Auto-configuring with CI 'Jenkins'
INFO: Load active rules
INFO: Load active rules (done) | time=10366ms
INFO: Indexing files...
INFO: Project configuration:
INFO: Excluded sources: vendor/**, resources/**, **/*.java
INFO: Load project repositories
INFO: Load project repositories (done) | time=573ms
INFO: 105 files indexed
INFO: 12 files ignored because of inclusion/exclusion patterns
INFO: 0 files ignored because of scm ignore settings
INFO: Quality profile for cs: Sonar way
INFO: Quality profile for flex: Sonar way
INFO: Quality profile for js: Sonar way
INFO: Quality profile for kotlin: Sonar way
INFO: Quality profile for php: Sonar way
INFO: Quality profile for py: Sonar way
INFO: Quality profile for web: Sonar way
INFO: Quality profile for xml: Sonar way
INFO: ------------- Run sensors on module sonarqubetest3
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=600ms
INFO: Sensor Python Sensor [python]
INFO: Starting global symbols computation
INFO: 9 source files to be analyzed
INFO: Starting rules execution
INFO: 9 source files to be analyzed
INFO: 9/9 source files have been analyzed
INFO: Sensor Python Sensor [python] (done) | time=6536ms
INFO: 9/9 source files have been analyzed
INFO: Sensor Cobertura Sensor for Python coverage [python]
INFO: Sensor Cobertura Sensor for Python coverage [python] (done) | time=23ms
INFO: Sensor PythonXUnitSensor [python]
INFO: Sensor PythonXUnitSensor [python] (done) | time=8ms
INFO: Sensor CSS Rules [cssfamily]
INFO: 2 source files to be analyzed
INFO: 2/2 source files have been analyzed
INFO: Sensor CSS Rules [cssfamily] (done) | time=1624ms
INFO: Sensor JaCoCo XML Report Importer [jacoco]
INFO: 'sonar.coverage.jacoco.xmlReportPaths' is not defined. Using default locations: target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml
INFO: No report imported, no coverage information will be imported by JaCoCo XML Report Importer
INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=6ms
INFO: Sensor Kotlin Sensor [kotlin]
INFO: 1 source files to be analyzed
INFO: Sensor Kotlin Sensor [kotlin] (done) | time=902ms
INFO: Sensor KotlinSurefireSensor [kotlin]
INFO: 1/1 source files have been analyzed
INFO: parsing [/var/jenkins_home/workspace/sonar-qube-testing/target/surefire-reports]
INFO: Sensor KotlinSurefireSensor [kotlin] (done) | time=5ms
INFO: Sensor JavaScript analysis [javascript]
INFO: 1 source files to be analyzed
INFO: Version of TypeScript used during analysis: 3.8.3
INFO: 1/1 source files have been analyzed
INFO: Sensor JavaScript analysis [javascript] (done) | time=4872ms
INFO: Sensor C# Properties [csharp]
INFO: Sensor C# Properties [csharp] (done) | time=1ms
INFO: Sensor JavaXmlSensor [java]
INFO: 8 source files to be analyzed
INFO: Sensor JavaXmlSensor [java] (done) | time=396ms
INFO: 8/8 source files have been analyzed
INFO: Sensor HTML [web]
INFO: Sensor HTML [web] (done) | time=80ms
INFO: Sensor Flex [flex]
INFO: 2 source files to be analyzed
INFO: Sensor Flex [flex] (done) | time=107ms
INFO: 2/2 source files have been analyzed
INFO: Sensor Flex Cobertura [flex]
INFO: No Cobertura report provided (see 'sonar.flex.cobertura.reportPaths' property)
INFO: Sensor Flex Cobertura [flex] (done) | time=1ms
INFO: Sensor XML Sensor [xml]
INFO: 8 source files to be analyzed
INFO: 8/8 source files have been analyzed
INFO: Sensor XML Sensor [xml] (done) | time=182ms
INFO: Sensor PHP sensor [php]
INFO: Starting PHP symbol indexer
INFO: 1 source files to be analyzed
INFO: 1/1 source files have been analyzed
INFO: Starting PHP rules
INFO: 1 source files to be analyzed
INFO: 1/1 source files have been analyzed
INFO: No PHPUnit test report provided (see 'sonar.php.tests.reportPath' property)
INFO: No PHPUnit coverage reports provided (see 'sonar.php.coverage.reportPaths' property)
INFO: Sensor PHP sensor [php] (done) | time=353ms
INFO: Sensor Analyzer for "php.ini" files [php]
INFO: Sensor Analyzer for "php.ini" files [php] (done) | time=2ms
INFO: Sensor VB.NET Properties [vbnet]
INFO: Sensor VB.NET Properties [vbnet] (done) | time=1ms
INFO: ------------- Run sensors on project
INFO: Sensor C# [csharp]
WARN: No protobuf reports found. The C# files will not have highlighting and metrics.
WARN: No Roslyn issue reports were found. The C# files have not been analyzed.
WARN: Your project contains C# files which cannot be analyzed with the scanner you are using. To analyze C# or VB.NET, you must use the Scanner for MSBuild 4.x, see https://redirect.sonarsource.com/doc/install-configure-scanner-msbuild.html
INFO: Sensor C# [csharp] (done) | time=1ms
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=16ms
INFO: CPD Executor 7 files had no CPD blocks
INFO: CPD Executor Calculating CPD for 8 files
INFO: CPD Executor CPD calculation finished (done) | time=28ms
INFO: Analysis report generated in 135ms, dir size=189 KB
INFO: Analysis report compressed in 77ms, zip size=70 KB
INFO: Analysis report uploaded in 581ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://192.168.0.4:9000/dashboard?id=sonarqubetest3
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://192.168.0.4:9000/api/ce/task?id=AXbH8OKO2xlvXb1FgSam
INFO: Analysis total time: 37.408 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 39.822s
INFO: Final Memory: 93M/761M
INFO: ------------------------------------------------------------------------
[Pipeline] }
[Pipeline] // withSonarQubeEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Quality Gates)
[Pipeline] sleep
Sleeping for 1 min 0 sec
[Pipeline] timeout
Timeout set to expire in 1 hr 0 min
[Pipeline] {
[Pipeline] waitForQualityGate
Checking status of SonarQube task 'AXbH8OKO2xlvXb1FgSam' on server 'sonarqube-server'
[Pipeline] }
[Pipeline] // timeout
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
org.sonarqube.ws.client.HttpException: Error 401 on http://192.168.0.4:9000/api/ce/task?id=AXbH8OKO2xlvXb1FgSam :
at org.sonarqube.ws.client.BaseResponse.failIfNotSuccessful(BaseResponse.java:36)
at hudson.plugins.sonar.client.HttpClient.getHttp(HttpClient.java:38)
at hudson.plugins.sonar.client.WsClient.getCETask(WsClient.java:51)
at org.sonarsource.scanner.jenkins.pipeline.WaitForQualityGateStep$Execution.checkTaskCompleted(WaitForQualityGateStep.java:234)
at org.sonarsource.scanner.jenkins.pipeline.WaitForQualityGateStep$Execution.start(WaitForQualityGateStep.java:171)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:286)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:179)
at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:122)
at sun.reflect.GeneratedMethodAccessor1239.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:163)
at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:157)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:161)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:165)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:135)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
at WorkflowScript.run(WorkflowScript:19)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:86)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)
at sun.reflect.GeneratedMethodAccessor1236.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.CollectionLiteralBlock$ContinuationImpl.dispatch(CollectionLiteralBlock.java:55)
at com.cloudbees.groovy.cps.impl.CollectionLiteralBlock$ContinuationImpl.item(CollectionLiteralBlock.java:45)
at sun.reflect.GeneratedMethodAccessor1659.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
at com.cloudbees.groovy.cps.Next.step(Next.java:83)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:185)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:400)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$400(CpsThreadGroup.java:96)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:312)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:276)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
我知道“错误401”表示凭据错误/未经授权,但相同的凭据在作业构建的第二阶段“SonarQube分析”中工作。
我还为SonarQube服务器中的Jenkins添加了Webhooks URL,作为“http://192.168.0.4:8056/sonarqube-webhook/“,但无济于事。
接下来我可以尝试什么?
2条答案
按热度按时间zujrkrfu1#
使用“withSonarQubeEnv”管道步骤时,提供的字符串值应与Jenkins中配置的SonarQube示例的名称匹配。在该配置中将定义sonarQube登录身份证明。如果该字符串与SonarQube示例名称匹配,则登录身份证明将被放入环境变量中。
但我相信这通常只期望使用“token”认证方法,而不是旧式的“login”和“password”值,在“token”方法中,“SONAR_AUTH_TOKEN”环境变量被设置为“sonar.login”属性,“sonar.password”值只是没有设置。
事实上,我认为在“waitForQualityGate”步骤中使用的“task”API只能使用“token”身份验证方法,要使其工作,您需要定义一个token值,并在SonarQube示例的Jenkins配置中设置该值。
虽然我们在构建中没有这样做,但我相信如果您在“withSonarQubeEnv”步骤中调用“waitForQualityGate”步骤,这将把适当的身份验证令牌放入环境变量中,以便“waitForQualityGate”可以看到它。
运行“sh”步骤非常方便,运行“env”命令行,这样您就可以看到在任何特定时间在环境中设置了哪些变量。
在www.example.com上阅读“声纳扫描仪”参考也是很好的,它讨论了这些步骤https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-jenkins/#AnalyzingwithSonarQubeScannerforJenkins-AnalyzinginaJenkinspipeline。
7nbnzgx92#
用这个
使用SonarQubeEnv(凭证ID:“您的令牌”)