Start Free
SonarQube Community Build | Analyzing source code | CI integration | Jenkins integration | Adding analysis to a Jenkins job

Adding SonarQube Community Build analysis to a Jenkins job

On this page

This section explains how to add the SonarQube Community Build analysis to your Jenkins Freestyle or Pipeline jobs. Note that you can also easily configure and analyze your projects with Jenkins in SonarQube Community Build through the tutorial in the application.

To be able to add a SonarQube Community Build analysis to a Jenkins job, Jenkins must have been set up for SonarQube Community Build integration

Adding analysis to a Freestyle job

The procedure depends on the project type.

  1. Create and configure your Jenkins job, and go to the Build Environment section.
  2. Enable Prepare SonarScanner environment to allow the injection of SonarQube Community Build values into this particular job. If multiple SonarQube Community Build instances are configured, you will be able to choose which one to use. Once the environment variables are available, use them in a standard Maven build step (Invoke top-level Maven targets) by setting the Goals to include, or a standard Gradle build step (Invoke Gradle script) by setting the Tasks to execute.

Maven goal:

$SONAR_MAVEN_GOAL

Gradle task:

sonar
  1. Create and configure your Jenkins job, and go to the Build section.
  2. Add the SonarQube for MSBuild - Begin Analysis to your build.
  3. Configure the SonarQube Project Key, Name, and Version in the SonarScanner for MSBuild - Begin Analysis build step.
  4. Add the compatible MSBuild build step or the Execute Windows batch command to execute the build.
  5. Add the SonarQube for MSBuild - End Analysis build steps to your build.
  1. Create and configure your Jenkins job, and go to the Build section.
  2. Add the SonarScanner CLI build step to your build.
  3. Configure the analysis properties. You can either point to an existing sonar-project.properties file or set the analysis properties directly in the Analysis properties field.

Adding analysis to a Pipeline job

  1. In Jenkins, create your Pipeline job.
  2. Add the SonarQube Community Build analysis stage to the Jenkins file: see below.
  3. Setup a pipeline pause until the quality gate is computed.

Adding analysis to a Multibranch Pipeline job

This feature is supported from SonarQube Server Developer Edition.

Adding an analysis stage to the Jenkins file

You must use the withSonarQubeEnv step in the SonarQube Community Build analysis stage of your pipeline job. This step is used to set the environment variables necessary to connect to the specified SonarQube Community Build instance. The connection details are retrieved from the Jenkins global configuration.

The withSonarQubeEnv() method can take the following optional parameters:

  • installationName (string): name of the SonarQube Community Build installation as configured in Jenkins.
    This is necessary if several SonarQube SonarQube Community Build hosts are configured in Jenkins.
  • credentialsId (string): if you want to overwrite the credentials configured in the Jenkins global configuration.
  • envOnly (boolean): set it to true if you only want the SonarQube Community Build environment variables to be expanded in the build context

Examples

Note that you don't need to specify an SCM stage in your Jenkins Pipeline or Multibranch Pipeline job.

Scripted pipeline example:

node {
  stage('SonarQube analysis') {
    withSonarQubeEnv() { // Will pick the global server connection you have configured
      sh './gradlew sonar'
    }
  }
}

Scripted pipeline example:

node {
  stage('SonarQube analysis') {
    withSonarQubeEnv(credentialsId: 'f225455e-ea59-40fa-8af7-08176e86507a', installationName: '<sonarqubeInstallation>') { // You can override the credential to be used, If you have configured more than one global server connection, you can specify the corresponding SonarQube installation name configured in Jenkins
      sh 'mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.11.0.3922:sonar'
    }
  }
}

Scripted pipeline example:

node {
  stage('Build + SonarQube analysis') {
    def sqScannerMsBuildHome = tool 'Scanner for .Net Framework'
    withSonarQubeEnv(<sonarqubeInstallation>') {// If you have configured more than one global server connection, you can specify its name as configured in Jenkins
      bat "${sqScannerMsBuildHome}\\SonarQube.Scanner.MSBuild.exe begin /k:myKey"
      bat 'MSBuild.exe /t:Rebuild'
      bat "${sqScannerMsBuildHome}\\SonarQube.Scanner.MSBuild.exe end"
    }
  }
}

Scripted pipeline example:

node {
  stage('SonarQube analysis') {
    def scannerHome = tool '<sonarqubeScannerInstallation>'; // must match the name of an actual scanner installation directory on your Jenkins build agent
    withSonarQubeEnv('<sonarqubeInstallation>') { // If you have configured more than one global server connection, you can specify its name as configured in Jenkins
      sh "${scannerHome}/bin/sonar-scanner"
    }
  }
}

Declarative pipeline example: 

pipeline {
  agent any
  stages {
    stage('SonarQube analysis') {
      steps {
        script {
            scannerHome = tool '<sonarqubeScannerInstallation>'// must match the name of an actual scanner installation directory on your Jenkins build agent
        }
        withSonarQubeEnv('<sonarqubeInstallation>') {// If you have configured more than one global server connection, you can specify its name as configured in Jenkins
          sh "${scannerHome}/bin/sonar-scanner"
        }
      }
    }
  }
} 

Was this page helpful?

© 2008-2024 SonarSource SA. All rights reserved. SONAR, SONARSOURCE, SONARQUBE, and CLEAN AS YOU CODE are trademarks of SonarSource SA.

Creative Commons License