<p>Create and add token to be able to connect to SonarQube.<br>
You have create project in SonarQube and use it as a parameter:</p>

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

<hr>

<p>Copied from <a href="https://stackoverflow.com/a/58626625/4944847">[StackOverflow] Problem with Connecting Jenkins to SonarQube</a>.</p>

CC BY-SA 4.0
2019-11-01T12:35:34.497Z
2019-11-01T12:35:34.497Z
agabrys
<p>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.</p>

<p>In Jenkins:</p>

<ul>
<li>I set up the sonarqube scanner at <code>Global Tool Configuration</code> as below:
<a href="https://i.sstatic.net/GQ7sd.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/GQ7sd.png" alt="enter image description here"></a></li>
<li>I generated a token in sonarqube and in Jenkins at <code>configuration</code> 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):
<a href="https://i.sstatic.net/wOOqu.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/wOOqu.png" alt="enter image description here"></a></li>
<li>In the jenkins pipeline this is how I added a stage for sonarqube:</li>
</ul>

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

<p>But this fails with below logs and <code>ERROR: script returned exit code 127</code>:</p>

<pre><code>[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
</code></pre>

<p>And when I check my jenkinstools on the disk sonnar plugin is not there:</p>

<pre><code>$ ls /var/lib/jenkins/tools/
jenkins.plugins.nodejs.tools.NodeJSInstallation
</code></pre>

<p>Can someone please let me know how I can connect Jenkins to sonarqube?</p>

<jenkins><jenkins-pipeline><jenkins-plugins><sonarqube>
Problem with Connecting Jenkins to SonarQube
AVarf
# Problem with Connecting Jenkins to SonarQube

(Asked by: username_0 on 2019-10-30)

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:

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):

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 sonnar 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?

**Tags:** jenkins, jenkins-pipeline, jenkins-plugins, sonarqube


---

## 1 answers

### Answer by username_1 on 2019-11-01. Score: 2

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=. \
"""


Copied from [StackOverflow] Problem with Connecting Jenkins to SonarQube.


---

