Devops Tutorial 27 - Multibranch pipeline in Jenkins
In real scenario, multiple developers would be working on their respective branches. For example, developer1 would be working on feature1 and developer2 would be working on feature2 and so on. All the developers might want to execute their Jenkinsfile on their respective branches. So here comes the need of Multibranch pipeline in Jenkins.
To start, just go to github repository that we created earlier and copy the HTTPS link of project as shown below
Note: The Jenkinsfile that you see above is part of main branch
Next, open git bash shell and execute the following steps to clone the Github repository to your local machine. The syntax is: git clone <project name that you copied above>
So we now have the remote project on our local machine. Let us cd to this project as shown below. We can now see that we are in ‘main’ branch and when we do ‘ls’, we see the Jenkinsfile as part of this main branch
We can see the contents of this file as shown below
‘git status’ command o/p is self-explanatory
‘git branch’ command o/p shows that we right now have only 1 branch viz main
Create a new branch feature_1
‘git branch’ command o/p now shows 2 branches and we are right now on branch main (shown by asterisk *)
Checkout the feature_1 branch in local machine
‘git branch’ command o/p now shows we are on branch feature_1 (shown by asterisk *).
Also notice below that we now see (feature_1) instead of (main)
Let us checkout Jenkinsfile from main branch into feature_1 branch
Edit the Jenkinsfile
Add an additional line
Save and close the file.
To double check, see the file contents, you should see the new line that you have added
Below command are self-explanatory
Push the Jenkinsfile to feature_1 branch
Copy above command and execute
You might be asked to enter Git credentials
You might get below error
To resolve this, generate token by following steps mentioned in below link
Execute the push command one more
Enter username and password (will be your token)
Click Ok
You might be to enter username and password again
Click Ok
Notice below that push is now successful
Refresh github.
You now see 2 branches, see below
Switch to feature_1 branch by clicking feature_1.
You can see Jenkinsfile in this branch as shown below
Click this file, you can see the new line that we had added
So we now have Jenkinsfile in 2 branches: main and feature_1
We will execute the Jenkinsfile on both of these branches in Jenkins.
To do that, create a Multibranch pipeline job in jenkins
Under 'Branch sources', select 'Git' from ‘Add source’ dropdown
Copy project link
In jenkins, paste it in project repository
Let the script path be Jenkinsfile, see below
Retain other default values.
Apply and save.
After sometime you would see 2 pipelines running (for the 2 branches)
Click ‘Scan Multibranch Pipeline Log’ that you see on left hand side
Notice in the logs below that Jenkinsfile is found and executed successfully on both the branches
Thank you for reading!