Jenkins waitForQualityGate fails because SonarCloud is returning outdated browser HTML response

Versions used:

  • SonarCloud
  • Scanner: SonarScanner for Maven (latest version)

Jenkinsfile snippet look like this:

        stage('Run Sonar Analysis') {
            steps {
                sh '''
                        rm -rf ${WORKSPACE}/.mvn/maven.config
                        mkdir -p ${WORKSPACE}/.mvn
                        touch ${WORKSPACE}/.mvn/maven.config
                        echo "-Dsonar.projectKey=${sonar_project_key}"                        >> ${WORKSPACE}/.mvn/maven.config
                        echo "-Dsonar.coverage.jacoco.xmlReportPaths=${jacoco_reports_path}"  >> ${WORKSPACE}/.mvn/maven.config
                        if [ -z "${CHANGE_ID}" ]; then
                            echo "-Dsonar.branch.name=${BRANCH_NAME}"                         >> ${WORKSPACE}/.mvn/maven.config
                        else
                            echo "-Dsonar.pullrequest.provider=GitHub"                        >> ${WORKSPACE}/.mvn/maven.config
                            echo "-Dsonar.pullrequest.github.repository=${sonar_github_repo}" >> ${WORKSPACE}/.mvn/maven.config
                            echo "-Dsonar.pullrequest.key=${CHANGE_ID}"                       >> ${WORKSPACE}/.mvn/maven.config
                            echo "-Dsonar.pullrequest.branch=${CHANGE_BRANCH}"                >> ${WORKSPACE}/.mvn/maven.config
                            echo "-Dsonar.pullrequest.base=${CHANGE_TARGET}"                  >> ${WORKSPACE}/.mvn/maven.config
                        fi
                    '''
                withSonarQubeEnv('SonarCloud') {
                    sh "mvn sonar:sonar"
                }
            }
        }

        stage('Verify Sonar Quality Gate') {
            steps {
                timeout(time: 10, unit: 'MINUTES') {
                    waitForQualityGate abortPipeline: true
                }
            }
        }

Analysis runs fine and I can see the results in the expected location in SonarCloud. Unfortunately, the waitForQualityGate fails with the following error. See attached error.log for complete error message.

hudson.remoting.ProxyException: net.sf.json.JSONException: Invalid JSON String
	at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:143)
	at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:103)
	at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:84)
	at hudson.plugins.sonar.client.WsClient.getCETask(WsClient.java:53)
	at org.sonarsource.scanner.jenkins.pipeline.WaitForQualityGateStep$Execution.checkTaskCompleted(WaitForQualityGateStep.java:238)
	at org.sonarsource.scanner.jenkins.pipeline.WaitForQualityGateStep$Execution.start(WaitForQualityGateStep.java:175)
	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 sun.reflect.GeneratedMethodAccessor6671.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: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:163)
	at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
	at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:158)
	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 com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
Caused: hudson.remoting.ProxyException: java.lang.IllegalStateException: Unable to parse response from https://sonarcloud.io//api/ce/task?id=AX_NBiu0cpJPsQaajMXK:
...

error.log (123.5 KB)

Hi,

When I see an “Invalid xxx String” message, I typically suspect an HTTP error.

So I tried your original URL (https://sonarcloud.io//api/ce/task?id=AX_NBiu0cpJPsQaajMXK) and got a 404. On closer inspection, you have a double slash after the domain. When I remove the extra slash I get a JSON-formatted permissions error (as I should). So that extra slash is the problem. The question is where it’s coming from. Can you double-check your ‘SonarCloud’ global configuration?

 
Ann

What configuration would you like me to check? For example, the mvn sonar:sonar command is creating this: https://sonarcloud.io/summary/new_code?id=oracle_weblogic-kubernetes-operator&pullRequest=2866

Hi,

You presumably have a global configuration that sets the location of & credentials to SonarCloud. That’s what I was hoping you’d check.

 
Ann

Hi,

So your server URL ends with a /. That’s probably the source of the double slash in the failing URL. Can you drop the slash and try again?

 
Ann

of course

That was it! I still feel like the waitForQualityGate code should handle the SonarQube URL with or without a trailing slash so I would still claim this is a bug but thanks so much for helping me work around the issue!