Multibranch Pipeline in Jenkins

  • January 27, 2023

In this tech article, we’ll cover the following topics:

  • What is a Multibranch Pipeline project?
  • How to create a Multibranch Pipeline project in Jenkins
  • What is a Webhook?
  • How to implement a Webhook on a Multibranch Pipeline project

What is a Multibranch Pipeline?

The Multibranch Pipeline project type enables you to implement different Jenkinsfiles for different branches of the same project. In a Multibranch Pipeline project, Jenkins automatically discovers, manages and executes Pipelines for branches that contain a Jenkinsfile in source control.

How to create a Multibranch Pipeline project in Jenkins

Step 1. Log in to Jenkins, add a new item and select Multibranch Pipeline.

NOTE: Make sure you’ve installed the required plugins: git integration plugin, GitHub integration plugin, Github branch source, maven Integration plugin, pipeline Maven Integration Plugin, Pipeline multibranch and build pipeline plugin.

Select Multibranch Pipeline

Step 2. Add the display and description as required (optional step).

Add the display and description

Step 3. Add the SCM URL, ID and password in the SCM credentials.

Add the SCM URL and id, password on credentials of SCM

Step 4. For the behaviors, multiple options are available to filter the branch. For now, keep it as default (Discover branch) so it automatically fetches the branch name.

Build Configuration: The mode is Jenkinsfile by default, which means only particular branches will be built with Jenkinsfile.

Build Configuration

Step 5. For the scan of the Multibranch Pipeline, use the “periodically if not otherwise run” option and set the interval based on the requirement. (We’ll discuss the Webhook option later.)

Set the interval

Step 6. Click Apply and then Save.

Step 7. Now, try to make some changes in the repository. These changes will scan and the pipeline will automatically start triggering.

Pipeline will automatically start triggering

Pipeline will automatically start triggering

What is a Webhook?

A Webhook is a mechanism to automatically trigger the build of a Jenkins project in response to a commit pushed to a Git repository.

How to implement a Webhook on a Multibranch Pipeline project

Step 1. Download the ngrok setup from https://ngrok.com/download.

Step 2. After successfully installing the setup, paste ngrok http 8080 in the ngrok command. This will return a forwarding URL that will help enable Webhook.

Forwarding URL in enabling Webhook

Step 3. Go to the repository and its settings -> Webhook. For the payload URL, paste the {forwarding url}/multibranch-webhook-trigger/invoke?token=mytoken code. Add the value of queryParams — any — based on your choice, and then do the same in Jenkins.

Go to the repository and its settings and Webhook

Step 4. Click Save Changes.

Step 5. Go back to Jenkins and for the Webhook add the plugin. Go to Dashboard -> manage Jenkins -> manage Plugins -> Available and search for Multibranch Scan Webhook Trigger.

Multibranch Scan Webhook Trigger

Step 6. After installing the plugin, restart Jenkins so the latest installation is visible. The change made in the Multibranch project is to disable “periodically if not otherwise run” and select “scan by Webhook.”

NOTE: Give the token the same name as the payload URL of GitHub.

Select for scan by Webhook

Step 7. Click Apply and then Save.

Step 8. Make some changes in the repository so that a Webhook will trigger and the pipeline will start automatically.

Webhook will trigger and the pipeline will start automatically

Webhook will trigger and the pipeline will start automatically

— By Jayant Mali