2
votes

I try to run sonar tests with maven in my Jenkins pipeline project. The documentations says if the sonar is configured globally and you use the withSonarQube step the environment variables with the globally configured sonar properites are injected. So far so good.

http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Jenkins#AnalyzingwithSonarQubeScannerforJenkins-AnalyzingwithSonarQubeScannerforMaven

My pipeline config looks like:

def stash = '********'
def branch = 'dev'

stage('git') {
    node {
        git branch: branch, credentialsId: 'Buildserver-Private.key', url: stash

    }
}

stage('build') {
    node {
        //....
    }
}

stage('sonar') {
    node {
        withSonarQubeEnv('Sonar') {
           sh 'mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar'
        } 
    }
}

The build fails because the sonar plugin trys to connect to the default h2 database instead of the configured one. If i check the log, there are no sonar properties passed to maven.

Injecting SonarQube environment variables using the configuration: Sonar
[Pipeline] {
[Pipeline] tool
[Pipeline] sh
[***********] Running shell script
+ cd .
+ /var/lib/jenkins/tools/hudson.tasks.Maven_MavenInstallation/Maven_3_3_9/bin/mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.2:sonar
[INFO] Scanning for projects...

[...]

[INFO] --- sonar-maven-plugin:3.2:sonar (default-cli) @ *******.project.build ---
[INFO] User cache: /var/lib/jenkins/.sonar/cache
[INFO] SonarQube version: 4.5.6
[INFO] Default locale: "en_US", source code encoding: "UTF-8" (analysis is platform dependent)
12:23:17.971 INFO  - Load global referentials...
12:23:18.071 INFO  - Load global referentials done: 102 ms
12:23:18.102 INFO  - User cache: /var/lib/jenkins/.sonar/cache
12:23:18.109 INFO  - Install plugins
12:23:18.176 INFO  - Install JDBC driver
12:23:18.183 INFO  - Create JDBC datasource for jdbc:h2:tcp://localhost/sonar

Why is my config ignored? What does the documentation mean if it says?

Since version 2.5 of the SonarQube Scanner for Jenkins, there is an official support of Jenkins pipeline. We provide a 'withSonarQubeEnv' block that allow to select the SonarQube server you want to interact with. Connection details you have configured in Jenkins global configuration will be automatically passed to the scanner.

It seems they are not ...

Has anybody an idea what am I missing?

1

1 Answers

3
votes

You are using an old version of SonarQube (4.5.6, the previous LTS) that requires to pass DB connection parameters (URL, login, password) to the scanners - which is a security issue. withSonarQubeEnv does not propagate those settings in order to fix this flaw.

Since SonarQube 5.2, these parameters are no longer required. So you have to use a version that is more recent. I suggest you to upgrade to the latest LTS version of SonarQube (5.6).