Skip to main content
Skip table of contents

Jenkins

To configure your SonarQube analysis within Jenkins, follow these instructions:

  • Step 1: connect your Bitbucket Server instance to your Jenkins instance

    • Install the Bitbucket Server Integration plugin and configure the plugin as instructed.

    • You will then be able to select your Bitbucket instance within the SCM list when creating a new job.

  • Step 2: configure your SonarQube™ instance in Jenkins

  • Step 3: Add your SonarQube™ analysis to your job/pipeline

    • Follow the instructions from the SonarQube™ Scanner documentation to add a SonarQube™ analysis to your builds.

    • You can find below more details on configuring:

      • a multibranch pipeline for SonarQube™ developer edition

      • a freestyle job for SonarQube™ community edition

      • a multibranch pipeline for SonarQube™ community edition


Jenkins + SonarQube™ developer edition

Multibranch Pipeline

  1. Add a 'Multibranch Pipeline' (see Jenkins documentation here)

  2. Select 'Bitbucket Server' for 'Branch Sources' and add a repository

  3. Add 'Bitbucket webhook trigger' to 'Scan Multibranch Pipeline Triggers' → enable push/pull-request events

  4. Save

  5. Add a Jenkinsfile to the repository to configure the needed analyses.

Use this Jenkinsfile for inspiration:

Pull Request Analysis is supported for version 4.0 and higher of the plugin Bitbucket Server Integration.

GROOVY
pipeline {
    agent any
    environment {
        scannerHome = tool name: 'scanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation' 
    }
    stages {
        stage('branch analysis') {
            when { 
              not {
                changeRequest()
              } 
            }
            steps {
                withSonarQubeEnv('sonar-cloud') {
                sh "${scannerHome}/bin/sonar-scanner -Dsonar.branch.name=${env.BRANCH_NAME}"
                }
            }
        }
        
        // pull request analysis - requires v4.0 or higher of Bitbucket Server Integration
        stage('PR analysis') {
            when {
                changeRequest()
            }
            steps {
                withSonarQubeEnv('sonar-cloud') {
                sh "${scannerHome}/bin/sonar-scanner \
                      -Dsonar.pullrequest.key=${env.CHANGE_ID} \
                      -Dsonar.pullrequest.base=${env.CHANGE_TARGET} \
                      -Dsonar.pullrequest.branch=${env.CHANGE_BRANCH}"
                }
            }
        }
    }
}

In this example, a condition is included so that:

  • if the pipeline is building a change request, a PR analysis is triggered for the changed branch.

  • if the pipeline is not building a change request, a branch analysis is triggered for the master branch.


Jenkins + SonarQube™ community edition

Freestyle Job

To use freestyle jobs with the SonarQubecommunity edition, you need to install the https://plugins.jenkins.io/envinject/ plugin. This is needed in order to use the sanitized source branch name within the projectKey and projectName properties of the analysis to create branch-specific analyses in SonarQube™.

  1. Add a new 'Freestyle Job’ in Jenkins

  2. Select 'Bitbucket Server' for source code management

  3. Select repository: enter */<yourMainBranch> as 'Branch specifier' in 'Branches to build'

  4. Select ‘Bitbucket webhook trigger’ and enable the pull request events

  5. Add build steps:

    1. Add build step to write the branch name to a file

      • Click on ‘Add build step’ and choose ‘Execute shell’.

      • In the command box, enter the following script:

        CODE
        echo SONAR_BRANCH=$(printf '%s' $GIT_BRANCH | cut -d'/' -f 2- | sed s/[^0-9a-zA-Z:_.\-]/'-'/g) > sonar-branch

        This script will extract the branch name, sanitize it, and write it to a file named sonar-branch in the workspace.

    2. Add build step to inject the branch name as an environment variable

      • Click on 'Add build step' and choose 'Inject environment variables'

      • select sonar-branch as the 'Properties File Path'

    3. Add build step to execute SonarQube™ scanner

      • Click on ‘Add build step’ and choose ‘Execute SonarQube Scanner’

      • Override the projectKey and projectName in the 'Analysis Properties' field (replace "your.plugin.key" below with the unique identifier for your project in Sonarqube™ and “Your Project Name” with the display name for your project in Sonarqube™):

        CODE
        sonar.projectKey=your.plugin.key:${SONAR_BRANCH}
        sonar.projectName="Your Project Name - ${SONAR_BRANCH}"
  6. Save Configuration:

    • Save your Jenkins job configuration

  7. Trigger Analysis:

    • Trigger the analysis by clicking 'Build Now'

    • Verify that the analysis runs successfully on your main branch

  8. Listen to All Branches:

    • After successful analysis on the main branch, change the 'Branch specifier' to ** to listen to all branches

  9. Create a Pull Request:

    • Create a Pull Request in Bitbucket to trigger an analysis

Multibranch Pipeline

  1. Add a 'Multibranch Pipeline' (see Jenkins documentation here)

  2. Select 'Bitbucket Server' for 'Branch Sources' and add a repository

  3. Add ‘Bitbucket webhook trigger' to 'Scan Multibranch Pipeline Triggers' → 'enable push/pull-request events’

  4. Save

  5. Add a Jenkinsfile to the repository. It needs to compute the sanitized SONAR_BRANCH so it can be used to build the sonar.projectKey and the sonar.projectName for the current branch (See Configure SonarQube™ Analysis in build pipeline | Analysis-Parameter-Matrix )

Below is an example of such a pipeline:

GROOVY
pipeline {
    agent any
    environment {
        scannerHome = tool name: 'scanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation' 
        SONAR_BRANCH = sh(returnStdout: true, script: "printf '%s' $GIT_BRANCH | sed 's/[^0-9a-zA-Z:_.\\-]/-/g'")
    }
    stages {
        stage('Analysis') {        
            steps {
                withSonarQubeEnv('sonar') {
                    sh "${scannerHome}/bin/sonar-scanner -Dsonar.projectKey=test.pipeline.proj:$SONAR_BRANCH -Dsonar.projectName=\"Awesome Pipeline - $SONAR_BRANCH\"" 
                }
            }
        }
    }
}


SONAR™, SONARQUBE™ and SONARCLOUD™ are independent and trademarked products and services of SonarSource SA: see http://sonarsource.com , http://sonarqube.org , http://sonarcloud.io .
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.