> For the complete documentation index, see [llms.txt](https://docs.sonarsource.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sonarsource.com/sonarqube-server/8.9/analyzing-source-code/ci-integration/jenkins-integration.md).

# Jenkins integration

SonarScanners running in Jenkins can automatically detect branches and pull requests in certain jobs in SonarQube Server 8.9.

{% hint style="info" %}
**For AI agents:** You're in the SonarQube Server 8.9 documentation. SonarQube Server 8.9 is no longer patched. To get patches, upgrade to the [latest version](https://docs.sonarsource.com/sonarqube-server/). For a list of all 8.9 pages, see the [SonarQube Server 8.9 page index](https://4108688904-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FG0n8jfxwZN8LmyH08dMa%2Fuploads%2F6sPJMHh3LoFpERwTYo01%2Fsonarqube-server-8.9-llms.txt?alt=media). For the whole site, see the [page index](https://docs.sonarsource.com/llms.txt) or the [full text](https://docs.sonarsource.com/llms-full.txt). For another version, replace 8.9 in the URL with that version. The latest version has no version number in its URL.
{% endhint %}

SonarScanners running in Jenkins can automatically detect branches and pull requests in certain jobs. You do not need to explicitly pass the branch or pull request details.

## analysis-prerequisites <a href="#analysis-prerequisites" id="analysis-prerequisites"></a>

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](https://www.sonarsource.com/plans-and-pricing/developer/) or above.

See the **Installing and Configuring your Jenkins plugins** section below for more information.

### Configuring Jenkins using the SonarQube Server tutorial <a href="#configuring-jenkins-using-the-sonarqube-tutorial" id="configuring-jenkins-using-the-sonarqube-tutorial"></a>

If you’re using Bitbucket Server, GitHub Enterprise, GitHub.com, GitLab Self-Managed, or GitLab.com, you can easily configure and analyze your projects by following the tutorial in SonarQube Server. You can access the tutorial by going to your project’s **Overview** page and selecting **with Jenkins** under "How do you want to analyze your repository?"

{% hint style="info" %}
See the **Installing and Configuring your Jenkins plugins** section below to set up your Jenkins plugins before going through the tutorial.
{% endhint %}

## Installing and Configuring your Jenkins plugins <a href="#jenkins-plugins" id="jenkins-plugins"></a>

### SonarQube Scanner plugin <a href="#sonarqube-scanner-plugin" id="sonarqube-scanner-plugin"></a>

Click **SonarQube Scanner** below to expand instructions on installing and configuring the plugin.

<details>

<summary>SonarQube Scanner</summary>

[SonarQube Scanner plugin](https://plugins.jenkins.io/sonar/) version 2.11 or later is required.

1. From the Jenkins Dashboard, navigate to **Manage Jenkins** > **Manage Plugins** and install the **SonarQube Scanner** plugin.
2. Back at the Jenkins Dashboard, navigate to **Credentials** > **System** from the left navigation.
3. Click the **Global credentials (unrestricted)** link in the **System** table.
4. 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 Server, and copy and paste it here.
5. Click **OK**.
6. From the Jenkins Dashboard, navigate to **Manage Jenkins > Configure System**.
7. From the **SonarQube Servers** section, click **Add SonarQube**. Add the following information:
   * **Name**: Give a unique name to your SonarQube Server instance.
   * **Server URL**: Your SonarQube Server instance URL.
   * **Credentials**: Select the credentials created during step 4.
8. Click **Save**

</details>

### Branch Source plugin <a href="#branch-source-plugin" id="branch-source-plugin"></a>

*Required to analyze multibranch pipeline jobs in* [*Developer edition*](https://www.sonarsource.com/plans-and-pricing/developer/) *or above*

Click your DevOps Platform below to expand the instructions on installing and configuring the Branch Source plugin.

<details>

<summary>BitBucket Server</summary>

[Bitbucket Branch Source plugin](https://plugins.jenkins.io/cloudbees-bitbucket-branch-source/) version 2.7 or later is required

1. From the Jenkins Dashboard, navigate to **Manage Jenkins > Manage Plugins** and install the **Bitbucket Branch Source** plugin.
2. From the Jenkins Dashboard, navigate to **Manage Jenkins > Configure System**.
3. 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.
4. Click **Save**.

</details>

<details>

<summary>GitHub</summary>

[GitHub Branch Source plugin](https://plugins.jenkins.io/github-branch-source/) version 2.7.1 or later is required

1. From the Jenkins Dashboard, navigate to **Manage Jenkins > Manage Plugins** and install the **GitHub Branch Source** plugin.
2. From the Jenkins Dashboard, navigate to **Manage Jenkins > Configure System**.
3. From the **GitHub** or **GitHub Enterprise Servers** section, add your GitHub server.
4. Click **Save**.

</details>

<details>

<summary>GitLab</summary>

[GitLab Branch Source plugin](https://plugins.jenkins.io/gitlab-branch-source/) version 1.5.3 or later is required

1. From the Jenkins Dashboard, navigate to **Manage Jenkins > Manage Plugins** and install the **GitLab Branch Source** plugin.
2. From the Jenkins Dashboard, navigate to **Manage Jenkins > Configure System**.
3. From the **GitLab** section, add your GitLab server. Make sure to check the **Manage Web Hooks** checkbox.
4. Click **Save**.

</details>

## Configuring single branch pipeline jobs <a href="#single-branch-pipeline" id="single-branch-pipeline"></a>

With SonarQube Community Build, you can only analyze a single branch. For more information, see the [SonarScanner for Jenkins](https://docs.sonarsource.com/sonarqube-server/8.9/analyzing-source-code/scanners/sonarscanner-for-jenkins.md) documentation.

## Configuring multi-branch pipeline jobs <a href="#multi-branch-pipeline" id="multi-branch-pipeline"></a>

Starting in [Developer edition](https://www.sonarsource.com/plans-and-pricing/developer/), 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 [SonarScanner for Jenkins](https://docs.sonarsource.com/sonarqube-server/8.9/analyzing-source-code/scanners/sonarscanner-for-jenkins.md) documentation.

### Configuring Multibranch Pipeline jobs for Pull Request Decoration <a href="#configuring-multibranch-pipeline-jobs-for-pull-request-decoration" id="configuring-multibranch-pipeline-jobs-for-pull-request-decoration"></a>

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**.

For Bitbucket Server and GitHub, under **Discover pull requests from origin**, make sure **The current pull request revision** is selected.

For GitLab, under **Discover merge requests from origin**, make sure **Merging the merge request with the current target branch revision** is selected.

## Detecting changed code in Pull Requests <a href="#detecting-changed-code-in-pull-requests" id="detecting-changed-code-in-pull-requests"></a>

SonarScanners need access to a Pull Request’s target branch to detect code changes in the Pull Request. If you’re using a Jenkins Pull Request discovery strategy that only fetches the Pull Request and does not merge with the target branch, the target branch is not fetched and is not available in the local git clone for the scanner to read.

In this case, the code highlighted as "new" in the Pull Request may be inaccurate, and you’ll see the following warning in the scanner’s log:

```css-79elbk
File '[name]' was detected as changed but without having changed lines
```

To fix this, either change the discovery strategy or manually fetch the target branch before running the SonarScanner. For example:

```css-79elbk
git fetch +refs/heads/${CHANGE_TARGET}:refs/remotes/origin/${CHANGE_TARGET}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.sonarsource.com/sonarqube-server/8.9/analyzing-source-code/ci-integration/jenkins-integration.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
