Jenkins integration
SonarScanners running in Jenkins can automatically detect branches and pull requests in certain jobs. You don't need to explicitly pass the branch or pull request details.
analysis-prerequisites
To run project analysis with Jenkins, you need to install and configure the following Jenkins plugins in Jenkins:
- The SonarQube Scanner plugin.
- The Branch Source plugin that corresponds to your DevOps Platform (Bitbucket Server, GitHub, or GitLab) if you're analyzing multibranch pipeline jobs in Developer Edition or above.
See the Installing and Configuring your Jenkins plugins section below for more information.
Installing and Configuring your Jenkins plugins
SonarQube Scanner plugin
Click SonarQube Scanner below to expand instructions on installing and configuring the plugin.
SonarQube Scanner
SonarQube Scanner plugin version 2.11 or later is required.
- From the Jenkins Dashboard, navigate to Manage Jenkins > Manage Plugins and install the SonarQube Scanner plugin.
- Back at the Jenkins Dashboard, navigate to Credentials > System from the left navigation.
- Click the Global credentials (unrestricted) link in the System table.
- Click Add credentials in the left navigation and add the following information:
- Kind: Secret Text
- Scope: Global
- Secret: Generate a token at User > My Account > Security in SonarQube, and copy and paste it here.
- Click OK.
- From the Jenkins Dashboard, navigate to Manage Jenkins > Configure System.
- From the SonarQube Servers section, click Add SonarQube. Add the following information:
- Name: Give a unique name to your SonarQube instance.
- Server URL: Your SonarQube instance URL.
- Credentials: Select the credentials created during step 4.
- Click Save
Branch Source plugin
Required to analyze multibranch pipeline jobs in Developer Edition or above
Click your DevOps Platform below to expand the instructions on installing and configuring the Branch Source plugin.
BitBucket Server
Bitbucket Branch Source plugin version 2.7 or later is required
From the Jenkins Dashboard, navigate to Manage Jenkins > Manage Plugins and install the Bitbucket Branch Source plugin. Then configure the following:
- From the Jenkins Dashboard, navigate to Manage Jenkins > Configure System.
- From the Bitbucket Endpoints section, Click the Add drop-down menu and select Bitbucket Server. Add the following information:
- Name: Give a unique name to your Bitbucket Server instance.
- Server URL: Your Bitbucket Server instance URL.
- Click Save.
BitBucket Cloud
Bitbucket Branch Source plugin version 2.7 or later is required
From the Jenkins Dashboard, navigate to Manage Jenkins > Manage Plugins and install the Bitbucket Branch Source plugin.
GitHub
GitHub Branch Source plugin version 2.7.1 or later is required
- From the Jenkins Dashboard, navigate to Manage Jenkins > Manage Plugins and install the GitHub Branch Source plugin.
- From the Jenkins Dashboard, navigate to Manage Jenkins > Configure System.
- From the GitHub or GitHub Enterprise Servers section, add your GitHub server.
- Click Save.
GitLab
GitLab Branch Source plugin version 1.5.3 or later is required
- From the Jenkins Dashboard, navigate to Manage Jenkins > Manage Plugins and install the GitLab Branch Source plugin.
- From the Jenkins Dashboard, navigate to Manage Jenkins > Configure System.
- From the GitLab section, add your GitLab server. Make sure to check the Manage Web Hooks checkbox.
- Click Save.
Configuring Jenkins through the SonarQube tutorial
You can easily configure and analyze your projects with Jenkins through the tutorial in SonarQube.
You need to set up SonarQube to import your repositories before accessing the tutorial. See the DevOps Platform Integrations in the left-side navigation of this documentation for more information.
A tutorial is currently available for all supported DevOps Platforms except Azure DevOps.
To access the tutorial:
- Click the Add project drop-down in the upper-right corner of the Projects page in SonarQube and select your DevOps platform.
- Select the repository you want to import into SonarQube.
- When asked How do you want to analyze your repository?, select With Jenkins.
See the Installing and Configuring your Jenkins plugins section below to set up your Jenkins plugins before going through the tutorial.
Configuring single branch pipeline jobs
With Community Edition, you can only analyze a single branch. For more information, see the Jenkins extension for SonarQube documentation.
Configuring multi-branch pipeline jobs
Starting in Developer Edition, you can analyze multiple branches and Pull Requests. The automatic configuration of branches and Pull Requests relies on environment variables available in Multibranch Pipeline jobs. These are set based on information exported by Jenkins plugins.
For configuration examples, see the Jenkins extension for SonarQube documentation.
Configuring Multibranch Pipeline jobs for Pull Request Decoration
You need to configure your Multibranch Pipeline job correctly to avoid issues with Pull Request decoration. From your Multibranch Pipeline job in Jenkins, go to Configure > Branch Sources > Behaviors and:
- Under Discover branches, make sure Exclude branches that are also filed as PRs (or MRs) is selected.
- Under Discover pull (or merge) requests from origin, make sure The current pull (or merge) request revision is selected.
- Under Specify ref specs, make sure the Ref Spec value includes any target branches (the default value should be enough).
If the Specify ref specs behavior is not active, click on Add and select Specify ref specs.
Was this page helpful?