jenkinssonarqubejenkins-pipelinejenkins-pluginssonarqube-scan

Problem with Connecting Jenkins to SonarQube


I have a Jenkins pipeline that periodically pull from gitlab and build different repos, build a multi-component platform, run and test it. Now I installed a sonarqube server on the same machine (Ubuntu 18.04) and I want to connect my Jenkins to sonarqube.

In Jenkins:

I set up the sonarqube scanner at Global Tool Configuration as below:

enter image description here

I generated a token in sonarqube and in Jenkins at configuration I set up the server as below BUT I couldn't find any place to insert the token (and I think this is the problem):

enter image description here

In the Jenkins pipeline this is how I added a stage for sonarqube:

stage('SonarQube analysis') {
    steps{
        script {
            scannerHome = tool 'SonarQube';
        }
        withSonarQubeEnv('SonarQube') {
            sh "${scannerHome}/bin/sonar-scanner"
        }
    }
}

But this fails with below logs and

ERROR: script returned exit code 127

[Pipeline] { (SonarQube analysis)
[Pipeline] script
[Pipeline] {
[Pipeline] tool
Invalid tool ID 
[Pipeline] }
[Pipeline] // script
[Pipeline] withSonarQubeEnv
Injecting SonarQube environment variables using the configuration: SonarQube
[Pipeline] {
[Pipeline] sh
+ /var/lib/jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/SonarQube/bin/sonar-scanner
/var/lib/jenkins/workspace/wws-full-test@tmp/durable-2c68acd1/script.sh: 1: /var/lib/jenkins/workspace/wws-full-test@tmp/durable-2c68acd1/script.sh: /var/lib/jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/SonarQube/bin/sonar-scanner: not found
[Pipeline] }
WARN: Unable to locate 'report-task.txt' in the workspace. Did the SonarScanner succeeded?
[Pipeline] // withSonarQubeEnv
[Pipeline] }
[Pipeline] // stage

And when I check my jenkinstools on the disk sonar plugin is not there:

$ ls /var/lib/jenkins/tools/
jenkins.plugins.nodejs.tools.NodeJSInstallation

Can someone please let me know how I can connect Jenkins to sonarqube?


Solution

  • Create and add token to be able to connect to SonarQube.
    You have create project in SonarQube and use it as a parameter:

    sh """
       ${scannerHome}/bin/sonar-scanner \
       -Dsonar.projectKey=your_project_key_created_in_sonarqube_as_project \
       -Dsonar.sources=. \
    """