In previous articles in this series, we looked into freestyle projects and pipelines in Jenkins. If you are not familiar with them, it would be better to brush up on those before diving into this.
- A Simple Freestyle Job in Jenkins
- Build a Docker Image with a Jenkins Freestyle Job
- A Simple Pipeline in Jenkins
What is a Multibranch Pipeline?
A multibranch pipeline is exactly what it sounds like. Suppose you have a project that has a few branches you have created to do some bug fixes or add new features. Before merging these branches back on to the master branch, you need to go through the same process of building, testing and hopefully deploying to a staging/testing environment.
Now if you had to create separate pipelines for all these branches, that kind of gets redundant and tedious, doesn’t it? That’s the problem a multi-branch pipeline solves. It allows you to create one job that would take care of all the branches. As long as there is a Jenkinsfile
in the branch, the pipeline will build that branch whenever the pipeline is triggered.
Create a Multibranch Pipeline in Jenkins
Creating a Multi-Branch Pipeline is so easy if you already know how to create a regular pipeline.
Create the Pipeline
- From Jenkins dashboard, click on “New Item” and select ”Multibranch Pipeline”. Give it an appropriate name as well and click OK. Since my project’s name is mobile-app-ws, I am going to give that as the project name. You can find the project here.
In the next screen, we can configure our job.
Configure Branch Sources
- Scroll down to the “Branch Sources” section and select Git from the “Add Sources” drop down.
- Fill in the repository URL and select the correct credentials from the drop down.
- In “Discover branches” section, click on “Add” button and select “Filter by name (with regular expression)”.
- Since we are going to build all the branches in this project, leave the regular expression as
.*
. Once everything is filled in correctly, “Branch Sources” section would look like below.
Double-Check the Details and Save
- Once you have verified everything is correct, you can just click on save button.
Once you save, the pipeline will automatically start scanning all the branches to see which ones have a Jenkinsfile
in them. And you will be redirected to “Scan Multibranch Pipeline Log” like below.
Allow this some time to finish and you would be able to notice the following in the log.
- Notice here that there are four branches in this repository.
Jenkinsfile
is present in the first three of them and therefore meets the criteria.- Because of that, Jenkins has scheduled a build for each of them.
- But the last one has no
Jenkinsfile
, as a result, does not meet the criteria. Therefore no build is scheduled for the branch.
Check the Multibranch Pipeline Status
Now if we go to Jenkins dashboard and click on our multibranch pipeline, we can see the status of each of our branches like below.
Here we can see, the build status of all the branches. As we saw in the log above, three branches have been built and they have been successful.
If you want to run another build, you can simply click on “Scan Multibranch Pipeline” from the navigator on left and it will scan all the branches for changes and schedule builds automatically. One important thing to note here is that if there are no changes in a particular branch, that branch will NOT be built! Only the branches that has some changes since the last build will be built.
If you want to check the status and logs of any of the individual branches, just click on the name of the branch.
Now this looks like a regular pipeline job, doesn’t it? You can click on “Build Now” to build just this branch whenever you need. Unlike when we scanned the pipeline above, in this case, “Build Now” works like a regular pipeline; meaning there is no need to have any changes for you to be able to build. Also you can click on the date under “Build History” to check the logs.
Hope this is enough about multibranch pipelines for someone to get started! You can play around and discover things on your own now.
In the next article, we would be looking into another very interesting topic related to Jenkins called “Shared Libraries”!