SonarQube 9.9: Quality gate error - Unable to guess SonarQube task id and/or SQ server details

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
    SonarQube 9.9 Developer Edition
    SonarScanner for MSBuild 5.5.3
    SonarQube Scanner Plugin for Jenkins v 2.15

  • what are you trying to achieve
    Scan project and Implement Quality gate to fail pipeline on Failure

Web hook has been setup at sonarqube and seems to be receiving data after each scan

  • what have you tried so far to achieve this
    Tried to troubleshoot and increase sonar debug level to see errors:

Part of the jenkins groovy file in question:

stage ('Build solution') {
            steps {
                dir('BlueSky') {
					withCredentials([string(credentialsId: 'xxx', variable: 'TOKEN')]) {
						withSonarQubeEnv('sonar') {
						bat """
							"${tool 'sonar'}\\SonarScanner.MSBuild.exe" begin /k:NRSBSS_phoenix /d:sonar.login=${TOKEN} /d:sonar.cs.opencover.reportsPaths=opencover.coverageresults.xml /d:sonar.verbose=true /d:sonar.pullrequest.key=${PR} /d:sonar.pullrequest.branch=${PR} /d:sonar.pullrequest.base=master /d:sonar.log.level=DEBUG || exit
							${tool 'nuget'} restore BlueSky.sln || exit
							"${tool 'MSBUILD15'}" BlueSky.sln /t:Build /p:Configuration=Debug /p:DebugType=pdbonly /p:ExcludeGeneratedDebugSymbol=false /p:DeployOnBuild=true /p:DeployTarget=Package || exit
						"""
						}
					}
				}
            }
        }
		stage('Complete SonarQube Scan') {
            steps {
                dir('BlueSky') {
                    withCredentials([string(credentialsId: 'xxx', variable: 'TOKEN')]) {						
                        bat """
                            "${tool 'sonar'}\\SonarScanner.MSBuild.exe" end /d:sonar.login=${TOKEN} || exit
                        """						
					}
                }
            }
        }		
		stage("Quality Gate") {
            steps {
				dir('BlueSky') {
					withCredentials([string(credentialsId: 'xxx', variable: 'TOKEN')]) {		
                        script{				
                            dir('BlueSky') {
                            timeout(time: 1, unit: 'HOURS') {
                                def qg = waitForQualityGate() 
                                if (qg.status != 'OK') {
                                error "Pipeline aborted due to quality gate failure: ${qg.status}"
                                }							
                            }
                            }
                        }
					}
				}
            }
        }
    }

Warning from Scanner log:
I see the Scanner “begin” complete successfully and then I see this warning:

[Pipeline] }
[2023-03-22T23:58:16.018Z] WARN: Unable to locate 'report-task.txt' in the workspace. Did the SonarScanner succeed?
[Pipeline] // withSonarQubeEnv
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Complete SonarQube Scan)
[Pipeline] dir

But later I see the file being written to a location by the “end” of the sonar scanner

[2023-03-23T00:05:39.438Z] 00:05:39.139 DEBUG: Report metadata written to D:\Jenkins\workspace\BlueSky SonarQube PR Testing\BlueSky\.sonarqube\out\.sonar\report-task.txt
[2023-03-23T00:05:39.438Z] 00:05:39.139 INFO: ANALYSIS SUCCESSFUL, you can find the results at: https://jenkins.dev.xxx.com/sonarqube/dashboard?id=NRSBSS_phoenix&pullRequest=1253
[2023-03-23T00:05:39.438Z] 00:05:39.139 INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
[2023-03-23T00:05:39.438Z] 00:05:39.139 INFO: More about the report processing at https://jenkins.dev.xxx.com/sonarqube/api/ce/task?id=AYcLyRBx-G0QqU2ZyDqz
[2023-03-23T00:05:39.438Z] 00:05:39.142 DEBUG: Post-jobs : 
[2023-03-23T00:05:39.438Z] 00:05:39.148 DEBUG: eslint-bridge server will shutdown
[2023-03-23T00:05:44.688Z] 00:05:44.149 DEBUG: eslint-bridge server closed
[2023-03-23T00:05:44.688Z] 00:05:44.150 INFO: Time spent writing ucfgs 0ms
[2023-03-23T00:05:49.948Z] 00:05:49.850 INFO: Analysis total time: 7:29.317 s
[2023-03-23T00:05:49.948Z] 00:05:49.854 INFO: ------------------------------------------------------------------------
[2023-03-23T00:05:49.948Z] 00:05:49.854 INFO: EXECUTION SUCCESS
[2023-03-23T00:05:49.948Z] 00:05:49.854 INFO: ------------------------------------------------------------------------
[2023-03-23T00:05:49.948Z] 00:05:49.854 INFO: Total time: 7:31.369s
[2023-03-23T00:05:50.205Z] 00:05:50.004 INFO: Final Memory: 86M/308M
[2023-03-23T00:05:50.205Z] 00:05:50.004 INFO: ------------------------------------------------------------------------
[2023-03-23T00:05:50.462Z] Process returned exit code 0
[2023-03-23T00:05:50.462Z] The SonarScanner CLI has finished
[2023-03-23T00:05:50.462Z] 00:05:50.306  Post-processing succeeded.

Error while executing quality gate stage:

[Pipeline] waitForQualityGate
[Pipeline] }
[Pipeline] // timeout
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] bat
[2023-03-23T00:05:52.260Z] 
[2023-03-23T00:05:52.260Z] D:\Jenkins\workspace\BlueSky SonarQube PR Testing>echo {"id": "1253", "state": "failure", "description": "Build job failed", "target_url": "https://jenkins.dev.xxx.com/job/BlueSky%20SonarQube%20PR%20Testing/62/"}  1>status.json 
[2023-03-23T00:05:52.260Z] 
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // timestamps
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.lang.IllegalStateException: Unable to guess SonarQube task id and/or SQ server details. Please use the 'withSonarQubeEnv' wrapper to run your analysis.
	at org.sonarsource.scanner.jenkins.pipeline.WaitForQualityGateStep$Execution.processStepParameters(WaitForQualityGateStep.java:215)
	at org.sonarsource.scanner.jenkins.pipeline.WaitForQualityGateStep$Execution.start(WaitForQualityGateStep.java:174)
	at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:322)
	at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:196)
	at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:124)
	at jdk.internal.reflect.GeneratedMethodAccessor2848.invoke(Unknown Source)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
	at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1225)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1034)
	at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:41)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
	at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:180)
	at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
	at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:163)
	at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:178)
	at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:182)
	at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:152)
	at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:152)
	at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:152)
	at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:152)
	at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:152)
	at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:152)
	at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
	at WorkflowScript.run(WorkflowScript:101)
	at ___cps.transform___(Native Method)
	at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:90)
	at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:116)
	at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixName(FunctionCallBlock.java:80)
	at jdk.internal.reflect.GeneratedMethodAccessor423.invoke(Unknown Source)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	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:152)
	at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:146)
	at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:136)
	at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:275)
	at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:146)
	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:187)
	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:420)
	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:330)
	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:294)
	at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:139)
	at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
	at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:68)
	at jenkins.util.ErrorLoggingExecutorService.lambda$wrap$0(ErrorLoggingExecutorService.java:51)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)
Finished: FAILURE

Hi,

You shouldn’t need both withCredentials and withSonarQubeEnv. Just the latter should do it.

Per the docs, you should unify the end step with the previous steps.

Can you give that a try and report back?

 
Ann

Hi Ann, Thank you for your reply,

I made the changes as per your comments and now the script looks like this:

stage ('Build solution') {
            steps {
                dir('BlueSky') {					
					withSonarQubeEnv('sonar') {
						bat """
							"${tool 'sonar'}\\SonarScanner.MSBuild.exe" begin /k:NRSBSS_phoenix /d:sonar.cs.opencover.reportsPaths=opencover.coverageresults.xml /d:sonar.verbose=true /d:sonar.pullrequest.key=${PR} /d:sonar.pullrequest.branch=${PR} /d:sonar.pullrequest.base=master /d:sonar.log.level=DEBUG || exit
							 ${tool 'nuget'} restore BlueSky.sln || exit
							"${tool 'MSBUILD15'}" BlueSky.sln /t:Build /p:Configuration=Debug /p:DebugType=pdbonly /p:ExcludeGeneratedDebugSymbol=false /p:DeployOnBuild=true /p:DeployTarget=Package || exit
                            "${tool 'sonar'}\\SonarScanner.MSBuild.exe" end || exit
						"""
					}					
				}
            }
        }		
		stage("Quality Gate") {
            steps {
				dir('BlueSky') {
					withSonarQubeEnv('sonar') {	
                        script{                            
                            timeout(time: 1, unit: 'HOURS') {
                                def qg = waitForQualityGate()
                                if (qg.status != 'OK') {
                                error "Pipeline aborted due to quality gate failure: ${qg.status}"
                                }
                            }
                        }
					}
				}
            }
        }

Now with this changes, I get a different error:
The token you provided doesn’t have sufficient rights to check license.

Below is the full log around the error:

11:54:24  D:\Jenkins\workspace\BlueSky SonarQube PR Testing\BlueSky>"D:\Jenkins\tools\hudson.plugins.sonar.MsBuildSQRunnerInstallation\sonar\SonarScanner.MSBuild.exe" begin /k:NRSBSS_phoenix /d:sonar.cs.opencover.reportsPaths=opencover.coverageresults.xml /d:sonar.verbose=true /d:sonar.pullrequest.key=1253 /d:sonar.pullrequest.branch=1253 /d:sonar.pullrequest.base=master /d:sonar.log.level=DEBUG   || exit
11:54:24  SonarScanner for MSBuild 5.5.3
11:54:24  Using the .NET Framework version of the Scanner for MSBuild
11:54:24  Default properties file was found at D:\Jenkins\tools\hudson.plugins.sonar.MsBuildSQRunnerInstallation\sonar\SonarQube.Analysis.xml
11:54:24  Loading analysis properties from D:\Jenkins\tools\hudson.plugins.sonar.MsBuildSQRunnerInstallation\sonar\SonarQube.Analysis.xml
11:54:24  sonar.verbose=true was specified - setting the log verbosity to 'Debug'
11:54:24  Pre-processing started.
11:54:24  Preparing working directories...
11:54:24  Using environment variables to determine the download directory...
11:54:25  15:54:24.87  15:54:24.859  Loading analysis properties from D:\Jenkins\tools\hudson.plugins.sonar.MsBuildSQRunnerInstallation\sonar\SonarQube.Analysis.xml
11:54:25  15:54:24.87  15:54:24.87  sonar.verbose=true was specified - setting the log verbosity to 'Debug'
11:54:25  15:54:24.874  Updating build integration targets...
11:54:25  15:54:24.88  The file SonarQube.Integration.ImportBefore.targets is up to date at D:\Users\jenkins\AppData\Local\Microsoft\MSBuild\4.0\Microsoft.Common.targets\ImportBefore
11:54:25  15:54:24.881  The file SonarQube.Integration.ImportBefore.targets is up to date at D:\Users\jenkins\AppData\Local\Microsoft\MSBuild\10.0\Microsoft.Common.targets\ImportBefore
11:54:25  15:54:24.881  The file SonarQube.Integration.ImportBefore.targets is up to date at D:\Users\jenkins\AppData\Local\Microsoft\MSBuild\11.0\Microsoft.Common.targets\ImportBefore
11:54:25  15:54:24.882  The file SonarQube.Integration.ImportBefore.targets is up to date at D:\Users\jenkins\AppData\Local\Microsoft\MSBuild\12.0\Microsoft.Common.targets\ImportBefore
11:54:25  15:54:24.882  The file SonarQube.Integration.ImportBefore.targets is up to date at D:\Users\jenkins\AppData\Local\Microsoft\MSBuild\14.0\Microsoft.Common.targets\ImportBefore
11:54:25  15:54:24.882  The file SonarQube.Integration.ImportBefore.targets is up to date at D:\Users\jenkins\AppData\Local\Microsoft\MSBuild\15.0\Microsoft.Common.targets\ImportBefore
11:54:25  15:54:24.883  The file SonarQube.Integration.ImportBefore.targets is up to date at D:\Users\jenkins\AppData\Local\Microsoft\MSBuild\Current\Microsoft.Common.targets\ImportBefore
11:54:25  15:54:24.886  Installed SonarQube.Integration.targets to D:\Jenkins\workspace\BlueSky SonarQube PR Testing\BlueSky\.sonarqube\bin\targets
11:54:25  15:54:24.887  Creating config and output folders...
11:54:25  15:54:24.888  Creating directory: D:\Jenkins\workspace\BlueSky SonarQube PR Testing\BlueSky\.sonarqube\conf
11:54:25  15:54:24.889  Creating directory: D:\Jenkins\workspace\BlueSky SonarQube PR Testing\BlueSky\.sonarqube\out
11:54:25  15:54:24.919  Downloading from http://xxx:9000/sonarqube/api/server/version...
11:54:25  15:54:24.953  Checking validity of server license
11:54:25  15:54:24.955  Downloading from http://xxx:9000/sonarqube/api/editions/is_valid_license...
11:54:25  15:54:24.971  The token you provided doesn't have sufficient rights to check license.
11:54:25  15:54:24.971  Pre-processing failed. Exit code: 1
[Pipeline] }
11:54:30  WARN: Unable to locate 'report-task.txt' in the workspace. Did the SonarScanner succeed?
[Pipeline] // withSonarQubeEnv
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Quality Gate)
Stage "Quality Gate" skipped due to earlier failure(s)

Hi,

Can you double-check your global sonar server configuration to make sure it has a valid token?

Also, check the docs for how to configure your wait step.

And finally, going back to your OP, it looks like your SonarScanner for .NET has a little age on it. The current version is 5.12. Can you upgrade?

 
Ann

Hi Ann, there was an issue with the token in global configuration in jenkins, we fixed it to map it to the correct credentials and now the quality gate works!
Thank you for all your help!!

1 Like