Skip to content

Jenkins With SonarQube#

Introduction#

SonarQube Configuration#

  • Firstly, you should access Sonarqube server. Then go to Administration --> Security --> Permission Template and check the checkbox for Execute Analysis for your Sonarqube account that you will use on Jenkins.
  • So for this configuration, the user will have permission to execute sonar analysis when we use this user on Jenkins Server.

 #zoom

  • Next you go to My Account --> Security and generate a Token as below. So this Token will be used for accessing SonarQube Server instead of using username/password.

 #zoom

  • You can try this token by replacing the username/password in pom.xml of your Spring Boot project.

 #zoom

pom.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
.....

<profiles>
        <profile>
                <id>dev</id>
                <properties>
                        <activeatedProperties>dev</activeatedProperties>
                        <sonar.host.url>http://localhost:9000</sonar.host.url>
                        <sonar.login>9da54180b98f2667d0858dd18ccdb79014bfd79f</sonar.login>
                </properties>
                <activation>
                        <activeByDefault>true</activeByDefault>
                </activation>
        </profile>
</profiles>
  • Next, you should go to Administration -> Configuration -> WebHooks and click Create button. Then add some information as Name and URL.
  • Note: the URL has format like {JENKINS SERVER}/sonarqube-webhook/. For exmaple: your Jenkins Server is running at http://192.168.1.18:8081 so the URL should be http://192.168.1.18:8081/sonarqube-webhook/.
  • Note: On Jenkins Server we have to add plugin Sonarqube Scanner which will do in the Step JENKINS CONFIGURATION below, then the URL above will be available.

 #zoom

Jenkins Configuration#

  • First, let’s go to Manage Jenkins -> Manage Plugins -> Then choose tag Available and find SonarQube Scanner -> then click Install

 #zoom

  • Next, go to the Manage Jenkins -> Global Tool Configuration -> scroll down to tag SonarQube Scanner and fill some information as below:

 #zoom

  • Next, go to Manage Jenkins -> Configure System -> scroll down to tag SonarQube servers and fill some information as NameURL of sonarqube server.

 #zoom

  • Then at step Server authentication token, let's choose button Add to add the SonarQube Token

 #zoom

  • Let's choose Secret Text, put your SonarQube Token, add a description and click add button.

 #zoom

  • Now, at step Server authentication token you can choose your SonarQube Token as the image below.

 #zoom

  • Finally, let's create/update your Jenkinsfile in your project as below.

 #zoom

build.Jenkinsfile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
pipeline {
    agent any
    tools {
        maven 'Maven 3.8.6'
        jdk 'JDK8'
    }
    stages {
        stage('Build') {
            steps {
                sh 'printenv'
                sh 'ls -la'
                sh 'mvn clean install -DskipTests'
            }
        }
        stage("Run Test") {
            steps {
              sh 'mvn test'
            }
        }
        stage("SonarQube Analysis") {
            steps {
              withSonarQubeEnv('SonarQube-Scanner') {
                sh 'mvn clean package sonar:sonar'
              }
            }
        }
        stage("Quality Gate") {
            steps {
              timeout(time: 1, unit: 'HOURS') {
                waitForQualityGate abortPipeline: true
              }
            }
        }
    }
}

Testing#

  • Finally, commit and push the Jenkinfile in your repository then go to Jenkins server to check the build result.

 #zoom

  • So, that’s it. Thanks and Good luck!

See Also#