Jenkins is a continuous integration tool which used to build and deploy applications. SonarQube is a Java based, open-source static code analysis tool. So, in this tutorial we are going to integrate SonarQube with Jenkins.

Prerequisites

  • SoanrQube up and running
  • You need a SonarQube server up and running. If you haven't SonarQube server, you can follow this tutorial and set up your SonarQube
  • Jenkins up and running
  • You need a Jenkins server up and running. If you haven't Jenkins server, you can follow this tutorial and set up your Jenkins

    Create SonarQube Token

    You need to login to SonarQube by giving your username and password. Then click on My Account->Security then under Tokens click on Create Token and give a name to the token. Then click on Generate button and copy the created token

    Install SonaQube Plugin

    Login to your Jenkins server

    Go to Manage Jenkins tab

    Then click on Manage Plugins

    Then select Available section and search for SonarQube Scanner for Jenkins plugin

    Configure SonaQube Plugin

    Go to Manage Jenkins tab

    Then click on Configure System

    Then SonarQube servers section, check on Environment variables

    Then add SonarQube as Name and YOUR_SONARQUBE_URL:9000 as Server URL and for Server authentication token add YOUR_SONARQUBE_TOKEN which we created earlier

    Then click on Save button

    Now we have finished up with our configuration. Now we need to create pipeline to check our configuration

    Jenkins Pipeline with SonarQube

    Create a new pipeline job in Jenkins and for pipeline script section, use the following code:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    pipeline {

    agent any

    stages {

    stage('checkout Code') {
    steps {
    checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/dinushchathurya/sonarqube-code-coverage.git']]])
    }
    }

    stage('SonaQube Analysis') {
    steps {
    withSonarQubeEnv('SonarQube'){
    sh './gradlew sonarqube'
    }
    }
    }
    }

    }

    Then run the pipeline

    Check code coverage

    Login to your SonarQube, click on Projects to see the project dashboard. You can see the code coverage report for the project.

    Conclusion

    In this tutorial, we learned how to integrate SonarQube with Jenkins. If you have any issue regarding this tutorial, mention your issue in comment section or reach me through my E-mail.

    Happy Coding