Skip to content

Sonarqube

Sonarqube

Installation:

  1. I followed this guide to install and start Sonarqube in localhost with port 9000
  2. I created a SonarScanner tool in Jenkins as shown in this guide
  3. To configure Sonarqube with jenkins I followed those steps, except for the Jenkinsfile sh commands, for that I followed this except for configuring sonar-project.properties file because for that I based on internet stuff and official documentation of Sonarqube.

sonar start

sonar localhost


Sonarqube configuration:

  1. Create project: Create project > manually > Write name project
  2. Enable scan code with sonar without force user credentials: Administration > Security > Force user authentication (disable) auth
  3. Set permissions to the project: Administration > Project > Management > global permissions
  4. Quality Gates: Quality Gates > create > "name"

    4.1 Add condition: You can create new conditions according of your needs and choose if you want to apply to "On new code" or "overall code".

    4.2 Grant permissions to the previous user you created to link it to this Quality Gate. permissions

Quality Gates Quality Gates 5. You can choose if select as default the new Quality Gate you created or select the one with name "Sonar Way" with the default metrics already has.


Jenkins configuration

  1. Plugin: Verify if Sonarqube plugin is already installed. plugin
  2. SQ server: Manage > Configuration > SonarQube servers > add server
  3. SQ scanner: Manage > Global Tool Configuration > SonarQube Scanner > add scanner

Jenkinsfile

This is how it looks the stages for Sonarqube:

    stage('Static Analysis - Sonarqube') {
        environment {
            SCANNER_HOME = tool 'SonarQubeScanner'
            PROJECT_NAME = 'SQA-Challenge'
         } 
        steps {
            echo 'running Sonarqube..'
                echo "${SCANNER_HOME}"
                withSonarQubeEnv('SonarQube') {
                sh '''
                    ${SCANNER_HOME}/bin/sonar-scanner \
                    -Dsonar.projectKey="${PROJECT_NAME}" 
                    '''
            } 
        } 
    }
    stage('Quality Gate - Sonarqube') {
        steps {
            echo 'waiting for QualityGate Sonarqube..'
                waitForQualityGate abortPipeline: true
        } 
    }

Results

sonar-jenkins results