{{announcement.body}}
{{announcement.title}}

Implement CI/CD for Multibranch Pipeline in Jenkins

DZone 's Guide to

Implement CI/CD for Multibranch Pipeline in Jenkins

A guide to creating a CI/CD pipeline with multiple branches.

Free Resource

tree branches

Making multiple branches with Jenkins.

Introduction

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.

Architecture Diagram

Architecture Diagram


Steps to Create a Simple Multibranch Pipeline Project

  1. Click New Item in the top left corner on the Jenkins dashboard.New Item
    New Item
  2. 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
    Multibranch pipeline
  3. Enter Description (optional).
  4. Add a Branch Source (for example, GitHub) and enter the location of the repository.
  5. Select the Add button to add credentials and click Jenkins
  6. Enter the GitHub username, Password, ID, and Description
  7. Select dropdown to add credentials in credentials field.Branch sources
    Branch sources
  8.  Click Save the Multibranch Pipeline project.
  9. 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
    Scan repository log
  10. Currently, this functionality exists only for GitHub and Bitbucket, with functionality provided by the GitHub Organization Folder and Bitbucket Branch Source plugins.
  11.  Once jobs are created build triggers automatically.Builds triggered
    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 

Webhooks

Webhooks


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. 

Settings

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.

Add webhook

Add webhook


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. 

Added webhook

Added webhook


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. CI triggers

CI triggers

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.

Console Output

Console Output

Further Reading

Learn How to Set Up a CI/CD Pipeline From Scratch

API Builder: A Simple CI/CD Implementation – Part 1

Topics:
jenkins ,cicd ,jenkins ci ,pipeline ,multipipeline ,jenkinsfile ,tutorial ,Devops

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}