Integration Jenkins with SonarQube

We have more option how to use SonarQube with Jenkins. At first I will show you how to use integration with tool Sonar Scanner on Jenkins

  • run my devops stack
  • generate token on SonarQube Server
  • project configuration in SonarQube
  • install on Jenkins server plugin SonarQube scanner
  • Configure Jenkins server for using SonarQube Scanner
  • Configure Jenkins server for using SonarQube Server
  • Create Pipeline on Jenkins server for running SonarQube scan
  • Integration with Maven and SonarQube

run my devops stack

follow this link and install devops stack

generate token on SonarQube Server

for generate new token on sonarqube server go to http://localhost:90/sonar/account/security/
(login and password is admin)

Generating SonarQube token

copy and save generated token for later use.

Install on Jenkins server plugin SonarQube Scanner

install on Jenkins server plugin “SonarQube scanner”

Configure Jenkins server for using SonarQube Scanner

Jenskins setting – SonarQube scanner

SONAR_RUNNER_HOME : /opt/sonar-scanner
This path comes from this Jenkinsfile. During the building “app_my_jenkins” image we are installing sonar scanner inside docker image and copy configuration file sonar-scanner.properties

configure credentials for access to SonarQube server from Jenkins

http://localhost:90/jenkins/credentials/store/system/domain/_/

Jenkins – Global credentials
Jenkins Credentials – Secret text

Secret is your SonarQube token

  • Manage Jenkins > Configure Systems > SonarQube Servers
    http://localhost:90/jenkins/configure
    • Name : SonarQube-Server
    • ServerURL : http://my-sonar:9000/sonar
    • Server authentication token: SonarQube Token
SonarQube Servers – Jenkins settings

ServerURL : http://my-sonar:9000/sonar
This URL comes from docker-compose.yml , service name of sonar

Create Pipeline on Jenkins server for running SonarQube scan

Go to the Jenkins server http://localhost:90/jenkins/blue/ and login with user name and password “admin“.

Jenkins BlueOcean – Create New Pipeline
Jenkins BlueOcean – Create New Pipeline from Git

Repository URL: https://github.com/devuserPP/SimpleCustomerApp.git

note: look at this file sonar-project.properties , where is definition what SonarQube should scan.

if you can see the result can go here http://localhost:90/jenkins/job/SimpleCustomerApp/job/master/

Jenkins – SonarQube link

as the SonarQube link has bad URL use this one http://localhost:90/sonar/dashboard?id=SimpleCustomerAppKey%3ASimpleCustomerApp and you should see something like this

SonarQube server – Report

Integration with Maven and SonarQube

In this case we do not need to install Sonar Scanner.
We only need maven installed on jenkins server or we can use docker container with maven as I used in this article.

https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-maven/

I want use very simple example, so I will start maven directly on Jenkins server.

Project configuration in SonarQube

Add a new project:
http://localhost:90/sonar/projects

Creating Jenkins Job.

http://localhost:90/jenkins/view/all/newJob

pipeline pipeline {
   agent any

   stages {
      stage('Build') {
         steps {
            // Get some code from a GitHub repository
            git 'https://github.com/devuserPP/simple-java-maven-app'

            // Run Maven on a Unix agent.
            sh "mvn clean verify sonar:sonar \
            -Dsonar.projectKey=My_Project \
            -Dsonar.host.url=http://my-sonar:9000/sonar \
            -Dsonar.login=ae6464011fa90bfc54adffc81927b6bf73c27632"

            // To run Maven on a Windows agent, use
            // bat "mvn -Dmaven.test.failure.ignore=true clean package"
            }
         }

         post {
            // If Maven was able to run the tests, even if some of the test
            // failed, record the test results and archive the jar file.
            success {
               junit '**/target/surefire-reports/TEST-*.xml'
               archiveArtifacts 'target/*.jar'
            }
         }
      }
   }

Leave a Reply

Close Menu