SonarQube and Declarative Pipeline

1,311 views
Skip to first unread message

Kirill Peskov

unread,
Jun 14, 2017, 7:31:26 AM6/14/17
to Jenkins Users
Hi All!

Has anyone manage to get SonarQube Scanner and Quality Gateway working
in a Declarative Pipeline? I'm actually stuck with that, because the
only examples on both Jenkins and SonarQube docs are examples for the
scripted pipeline, not declarative one...


SonarQube document:
https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Jenkins
Jenkins document: https://jenkins.io/doc/pipeline/steps/sonar/


Both examples are for scripted pipeline (documentation bug/lint?) and
neither of them explains what is actually necessary inside the block
withSonarQubeEnv{} and how to start the scanner (which is located on the
agent in the 'tools' subfolder. Even more — Jenkins does not recognize
withSonarQubeEnv{} as 'step' at all :(


I think one working example would be enough to understand... To add
another 5 cents, I have to pass extra -Xms and -Xmx parameters to the
scanner, otherwise it crashes with OOM (I tested it in a 'freestyle' job).


Cheers,
Kirill

Kirill Peskov

unread,
Jun 14, 2017, 10:41:55 AM6/14/17
to jenkins...@googlegroups.com
OK, I went a bit further, wrapping scripted pipeline syntax pieces in
script{} pseudo-step, so I actually created this:


stage('Submit the sources to SonarQube') {
environment {
SONAR_SCANNER_OPTS = "-Xmx2G -Xms256m"
}
steps {
script {
def scannerHome = tool 'SonarQube Scanner';
withSonarQubeEnv('SonarQube Local') {
sh "${scannerHome}/bin/sonar-scanner"
}
}
}
}

But later on I stuck again, because the agent was not able to find the
sonar-scanner, despite properly formed path to it. I fiddled with that a
bit and found, that the path actually existed on the agent only (docker
host), but was never exposed to the container, where the scanner suppose
to be started according to Mr. Jenkins logic (actually I see no point to
start the sonar-scanner from within the container, checkout happens on
the docker host anyway, so firing up the scanner on the docker host
itself would be much more elegant). So I added 'arg' to my dockerfile
part of the pipeline:

agent {
dockerfile {
dir 'ubuntu-xenial-mysql'
args '-v /Jenkins-CI/tools:/Jenkins-CI/tools'
}
}

So the tools would become available not only on 'persistent' docker host
agent machine, but inside the container as well. For now — submission
has been successful, waiting the analysis and if webhook would work.

Cheers,
Kirill
Reply all
Reply to author
Forward
0 new messages