Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure a Jenkins Pipeline for SonarQube scan

I am trying to configure a jenkins pipeline for my project, but there is something missing here, if anyone could direct on what I am doing wrong:

Below is the pipeline script:

node {
    stage('SonarQube analysis') {
    // requires SonarQube Scanner 2.8+
    def scannerHome = tool 'sonarScanner';
    withSonarQubeEnv('SonarQube 6.2') {
      bat "${scannerHome}/bin/sonar-runner.bat"
    }
  } 
}

Below is the Jenkins Sonar Plugin related configurations: Under Manage Jenkins > Configure System: enter image description here

Under Manage Jenkins > Global Tool Configuration enter image description here

Below is the error that I get:

D:\jenkins\workspace\Test_Pipeline>D:\sonar-runner-2.4/bin/sonar-runner.bat
D:\sonar-runner-2.4
SonarQube Runner 2.4
Java 1.8.0_92 Oracle Corporation (64-bit)
Windows Server 2008 R2 6.1 amd64
INFO: Runner configuration file: D:\sonar-runner-2.4\conf\sonar-runner.properties
INFO: Project configuration file: NONE
INFO: Default locale: "en_US", source code encoding: "UTF-8"
INFO: Work directory: D:\jenkins\workspace\Test_Pipeline\.\.sonar
INFO: SonarQube Server 6.2
19:48:53.627 INFO  - Load global repositories
19:48:53.920 INFO  - Load global repositories (done) | time=298ms
19:48:53.951 WARN  - Property 'sonar.jdbc.url' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
19:48:53.951 WARN  - Property 'sonar.jdbc.username' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
19:48:53.951 WARN  - Property 'sonar.jdbc.password' is not supported any more. It will be ignored. There is no longer any DB connection to the SQ database.
19:48:53.951 INFO  - User cache: C:\Users\Administrator\.sonar\cache
19:48:54.378 INFO  - Load plugins index
19:48:54.378 INFO  - Load plugins index (done) | time=0ms
19:48:55.235 INFO  - Process project properties
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
Total time: 3.404s
Final Memory: 5M/120M
INFO: ------------------------------------------------------------------------
ERROR: Error during Sonar runner execution
ERROR: Unable to execute Sonar
ERROR: Caused by: You must define the following mandatory properties for 'Unknown': sonar.projectKey, sonar.sources
ERROR: 
ERROR: To see the full stack trace of the errors, re-run SonarQube Runner with the -e switch.
ERROR: Re-run SonarQube Runner using the -X switch to enable full debug logging.
[Pipeline] }
[Pipeline] // wrap
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

The problem seems that Jenkins does not know where my sonar-project.properties is placed. It is under D:\myprj and has contents as below:

# required metadata
sonar.projectKey=my_prj:my_prj
sonar.projectName=my_prj
sonar.projectVersion=1.00
sonar.sources=my_prj/src
sonar.language=java

Pls suggest how I can make Jenkins pipeline aware of where the sonar-project.properties file is placed

like image 443
user5917011 Avatar asked Jan 06 '17 20:01

user5917011


2 Answers

I needed to add the workspace to my script. Here is the script below:

stage('SonarQube analysis') {
    ws('D:\\my_prj') {
    // requires SonarQube Scanner 2.8+
    def scannerHome = tool 'sonarScanner';
    withSonarQubeEnv('SonarQube 6.2') {
      bat "${scannerHome}/bin/sonar-scanner.bat"
    }
  }
}
like image 195
user5917011 Avatar answered Oct 21 '22 09:10

user5917011


I see you were using Sonar-runner 2.4. I think by running 2.8 version you will have better results. This is my JenkinsFile configuration, by using Declarative Pipeline syntax. I was having the same issue, but I solved it by renaming my sonar-project.properties file, the previous name was "sonar.properties". I hope this may help others.

stage('Sonarqube analysis') {
    steps {
        script {
            scannerHome = tool 'SonarScanner';
        }
        withSonarQubeEnv('SonarQube') {
            bat "${scannerHome}/bin/sonar-scanner.bat" 
        }
    }
}
like image 28
Vivi Ch Avatar answered Oct 21 '22 10:10

Vivi Ch