Skip to content

Integrating Sonarqube with Jenkins

Deekshith SN edited this page Oct 9, 2021 · 4 revisions

Pre-requisite

  • We need to have Jenkins and sonarqube server up and running (by default Jenkins runs on 8080 and sonarqube at 9000), to install on ubuntu refer link

Initial setup

In Jenkins install plugins like docker, sonarqube and Sonar quality gate plugin (manage Jenkins --> manage plugins --> in available tab search for respective plugins) After sonar plugin installation in configure system enter sonar information as shown in below picture

jenkins1

  • Name: any meaningful name, but this will be referred in pipeline while executing sonar steps
  • Server URL: sonarqube url
  • Sever authentication token: this token has to be created in sonarqube To create that token in sonarqube navigate to administration --> Security --> Users ( after which you will see list of users)

sonar3

Click on token button, you will prompted to create token as shown in below pictures

sonar1

Give meaningful name and click on generate

sonar2

Copy the token to create secret text in jenkins

sonar4

In Jenkins navigate manage Jenkins --> manage credentials --> add credentials --> select kind as secret text --> give value in secret ( will show up in sonar authentication token dropdown)

jenkins2

Also need to create webhook to have communication between jenkins and sonarqube To create webhook navigate to administration --> configuration --> webhooks --> then provide name and url of your jenkins sufixed with /sonarqube-webhook/

sonar5

 timeout(time: 1, unit: 'HOURS') {
                      def qg = waitForQualityGate()
                      if (qg.status != 'OK') {
                           error "Pipeline aborted due to quality gate failure: ${qg.status}"
                      }
                    }

Then in sonarqube configure quality gates, below is default if want we can change it

s0nar6

Jenkinsfile would look like this, as you can see we are verifying our code against sonarqube quality gate, it code satisfies with quality gate then it will procced further else it will stop build in that stage

jenkisn3

If you are quality gate satisfices then you will see passed in jenkins page

jenkins4