Implement CI/CD for Multibranch Pipeline in Jenkins
A guide to creating a CI/CD pipeline with multiple branches.
Join the DZone community and get the full member experience.
Join For FreeIntroduction
Jenkins is a continuous integration server that can fetch the latest code from the version control system (VCS), build it, test it, and notify developers. Jenkins can do many things apart from just being a Continuous Integration (CI) server. Originally known as Hudson, Jenkins is an open-source project written by Kohsuke Kawaguchi. As Jenkins is a Java-based project, before installing and running Jenkins on your machine, first, you need to install Java 8.
The Multibranch Pipeline allows you to automatically create a pipeline for each branch on your Source Code Management (SCM) repository with the help of Jenkinsfile.
What is a Jenkinsfile?
Jenkins pipelines can be defined using a text file called Jenkinsfile. You can implement pipeline as code using Jenkinsfile, and this can be defined by using a domain-specific language (DSL). With Jenkinsfile, you can write the steps needed for running a Jenkins pipeline.
You may also enjoy: Building a Continuous Delivery Pipeline Using Jenkins
Definition from Jenkins
The Multibranch Pipeline project type enables you to implement different Jenkinsfile for different branches of the same project. In a Multibranch Pipeline project, Jenkins automatically discovers, manages and executes Pipelines for branches that contain an in-source control.
Steps to Create a Simple Multibranch Pipeline Project
- Click New Item in the top left corner on the Jenkins dashboard.
New Item - Enter the name of your project in the Enter an item name field, scroll down, and select Multibranch Pipeline and click OK button.
Multibranch pipeline - Enter Description (optional).
- Add a Branch Source (for example, GitHub) and enter the location of the repository.
- Select the Add button to add credentials and click Jenkins.
- Enter the GitHub username, Password, ID, and Description
- Select dropdown to add credentials in credentials field.
Branch sources - Click Save the Multibranch Pipeline project.
- Jenkins automatically scans the designated repository and does some indexing for organization folders. Organization Folders means enable Jenkins to monitor an entire GitHub Organization or Bitbucket Team/Project and automatically create new Multibranch Pipelines for repositories that contain branches and pull requests containing a Jenkinsfile.
Scan repository log - Currently, this functionality exists only for GitHub and Bitbucket, with functionality provided by the GitHub Organization Folder and Bitbucket Branch Source plugins.
- Once jobs are created build triggers automatically.
Builds triggered
Configure Webhooks
12. We have to configure our Jenkins machine to communicate with our GitHub repository. For that, we need to get the Hook URL of the Jenkins machine.
13. Go to Manage Jenkins and select the Configure System view.
14. Find the GitHub Plugin Configuration section and click on the Advanced button.
15. Select the Specify another hook URL for GitHub configuration
16. Copy URL in the text box field and unselect it.
17. Click Save it will redirect to the Jenkins dashboard.
18. Navigate to the GitHub tab on the browser and select your GitHub repository.
19. Click on Settings. It will navigate to the repository settings.
20. Click on the Webhooks section.
21. Click on Add Webhook button. Paste the Hook URL on the Payload URL field.
22. Make sure the trigger webhook field has Just the push event option selected.
23. Click Add webhook and it will add the webhook to your repository.
24. Once you've added a webhook correctly, you can see the webhook with a green tick.
25. Go back to the repository and change to the Branch and Update any of the files. In this scenario, we update the README.md file.
26. Now see that the Jenkins job will trigger automatically.
27. After pipeline execution is completed, we can verify the history of executed build under the Build History by clicking the build number.
28. Click on build number and select Console Output. Here you can see the outputs in each step.
Further Reading
Opinions expressed by DZone contributors are their own.
Comments