withSonarQubeEnv in Jenkins pipeline not setting sonar qube env for maven project

6,506 views
Skip to first unread message

jonwi...@yahoo.com

unread,
Oct 11, 2017, 11:04:47 PM10/11/17
to SonarQube
Using the "withSonarQubeEnv" step in Jenkins pipeline (declarative syntax) running maven code coverage and sonar analysis results in a "[ERROR] SonarQube server [http://localhost:9000] can not be reached" message.  Other than running additional maven goals the pipeline is setup based on the https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Jenkins#AnalyzingwithSonarQubeScannerforJenkins-AnalyzinginaJenkinspipeline docs.  Is the 

stages {
stage("Build") {
steps {
container('default') {
// change into app directory
dir('app') {
withSonarQubeEnv('my-sonar') {
                        // there aren't any sonar env variables printed out
sh 'printenv'
sh "mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install org.jacoco:jacoco-maven-plugin:report org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -P !sonar"
}
}
}
}
}
stage("Code Quality Gate") {
steps {
container('default') {
echo "Waiting for sonar results..."
script {
def qg = waitForQualityGate()
if (qg.status != 'OK') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
}
}
}
}
}
}
SonarQube Scanner for Jenkins v2.6.1

mvn --version
Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 1.8.0_141, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en_US, platform encoding: ANSI_X3.4-1968
OS name: "linux", version: "4.13.5-coreos-r1", arch: "amd64", family: "unix"

Jenkins ver. 2.73.1

G. Ann Campbell

unread,
Oct 12, 2017, 8:46:06 AM10/12/17
to SonarQube
Hi,

I take it that you've configured a SonarQube server at the global level that's named "my-sonar"?


Ann

P.S. Please be aware that the standard courtesies (Hi, Thanks, ...) are appreciated in this group.

jonwi...@yahoo.com

unread,
Oct 12, 2017, 10:35:14 AM10/12/17
to SonarQube
HI Ann,

Yes, I've configured the SonarQube server in the global Jenkins config and added the webhook to our sonar instance.  I do see the following logging which seems to confirm it's able to find the global config:

[Pipeline] dir
Running in /home/jenkins/workspace/infor-customer-adapter/pull-request-pipeline/app
[Pipeline] {
[Pipeline] withSonarQubeEnv
Injecting SonarQube environment variables using the configuration: my-sonar
[Pipeline] {

The only other piece of the puzzle is we're using the https://github.com/jenkinsci/kubernetes-plugin to launch nodes from the Jenkins master.

Thanks,
Jon

G. Ann Campbell

unread,
Oct 12, 2017, 10:51:44 AM10/12/17
to jonwi...@yahoo.com, SonarQube
Hi Jon,

This part catches my eye:
org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
Why not use `sonar:sonar`? And could you give that a try?


Ann




---
G. Ann Campbell | SonarSource
Product Manager
@GAnnCampbell

--
You received this message because you are subscribed to a topic in the Google Groups "SonarQube" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sonarqube/f-yM1A4huQI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sonarqube+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/ed1cbd15-b210-47c8-969b-0b9e11ec4b9e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jonwi...@yahoo.com

unread,
Oct 12, 2017, 1:18:04 PM10/12/17
to SonarQube
Hi Ann,

I actually started with sonar:sonar since that's what we'd used in the past.  When I got the same error (not able to find localhost:9000 sonarqube) I updated to better align with the documentation on https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Jenkins#AnalyzingwithSonarQubeScannerforJenkins-AnalyzinginaJenkinspipeline.

Thanks,
Jon
To unsubscribe from this group and all its topics, send an email to sonarqube+...@googlegroups.com.

jonwi...@yahoo.com

unread,
Oct 13, 2017, 5:46:27 PM10/13/17
to SonarQube
Running this in a Jenkins v 2.73.2 docker image locally with agent set to "any" runs the job on the master node and the printenv command within the withSonarQubeEnv block does contain SONAR_HOST_URL, SONAR_CONFIG_NAME and SONAR_MAVEN_GOAL env variables. Theses are not present when run on our kubernetes build agent. I seem to recall some comment about the sonar qube jenkins plugin not working on Jenkins slaves. Is this perhaps a special variant of that problem? Below is our agent definition within the pipeline.

agent {
kubernetes {
label 'posKubekinsAgent'
inheritFrom 'default'
containerTemplate {
name 'default'
image 'agent-image-here'
}
}
}

tid...@gmail.com

unread,
Dec 6, 2017, 8:57:15 AM12/6/17
to SonarQube
Hi Jon!

I've the same issue between Kubernetes Plugin and SonarQube using declarative syntax.
Have you managed to solve it?

Thanks

l...@computas.com

unread,
Dec 19, 2017, 3:35:51 AM12/19/17
to SonarQube
Hi!

Having the same issue here. It only appears to be a problem when using the kubernetes closure for defining the agent. If you create a podTemplate in the Jenkins System Configuration, label it and use that for an agent, things seems to be in order. I recall having a similar experience when using wrap(), so this might be an issue in the kubernetes-plugin more than in the SonarQube plugin.

- LM

carl...@gmail.com

unread,
Feb 19, 2018, 4:19:32 AM2/19/18
to SonarQube
See open issue in kubernetes plugin https://issues.jenkins-ci.org/browse/JENKINS-49110 

It should work if you put withSonarQubeEnv outside the container step
Reply all
Reply to author
Forward
0 new messages