Before starting with a multibranch pipeline using jenkins. Lets first see what is jenkins.
Jenkins: Is a free and open-source automation server or we can say it tool. It helps to automate the parts of software development related to building, testing and deploying, facilitating continuous integration and continuous delivery.
So we can say that jenkins is a tool which is used to create a CI-CD pipeline for our application.
We are not going to discuss about the Jenkins over here in detail . We are going to see how multibranch pipeline can be created using jenkins and how to trigger multibranch pipline automaticaly whenever new commit or pushe’s or pr is generated.
If you hav no idea about jenkins and you want learn , you can refer HERE
Let’s start with the Multibranch Pipeline:
Mutibranch Pipeline:
WHY ?
Let’s first disscuss undersatnd why we need multibranch pipeline. As whenever we create a simple pipeline we are able to work on the single branch that we specify in the pipeline , the default one master. So imagine we have multiple enviroment and need to deploy application on those environment i.e test, develop and production. So in that case will you create the multiple single-branch pipeline ? (i.e one for test, one for develop and master)
Ans: No .
WHAT ?
So the solution for above case is Multibranch pipeline. Multibranch pipeline allows to create a single pipeline for the differant branches. So whenever you hit a particular branch , the pipeline get start to execute for that branch only(i.e execute the stages you specify in your jenkinsfile)
HOW ?
In this section we are going to see how to create a multibranch pipeline and trigger automatically using github-webhook.
Step1:
Go to jenkinse -> New Item -> Enter item name -> Select Mutibranch Pipeline -> OK
Step2:
You see a display name and description field, in General Section, you can provide a description of your build or whatever you want. You can keep it empty also.
Step3:
From the Branch Sources section select GitHub rather than git -> Provide repository URL. In case repo is public it’s fine but if the repo is private, you need to provide the credentials otherwise it gives an error.
Step4:
In the Build Configuration keep mode by jenkinsfile as it is. Generally we create a jenkinsfile in project directory . if You are you creating at another location you need to specify the path of your jenkinsfile in Script path . Other keep it as it is.
Step5:
In Scan Multibranch Pipeline Triggers you can select the inetrval that you want to trigger your pipeline. But as we are using Github-Webhook for now do not this option -> Click on save.
Now your Multibranch pipeline is created successfully. Click on Build and you can see all branch that contains Jenkins file gets executed by Jenkins.
Configure Webhook
Now how to trigger pipeline whenever push, commit, or PR is generated on the specified repo.
Step1: Go to Manage Jenkins and select the Configure System view.
Step2: Find the GitHub Plugin Configuration section and click on the Advanced button.
Step3: Select the Specify another hook URL for GitHub configuration -> Copy URL and unselect it. -> Click Save.
Step4: Now You need to create the github-webhook. To create webhook follow the steps given below.
Step2: Go to your project repository -> Go to “settings” in the right corner -> Click on “webhooks” -> Click “Add webhooks” -> Paste the Payload URL as
Which events would you like to trigger this webhook?
- Just the push event: Only send data when someone pushed into my repository.
- Send me everything: If there is any pull or push event in our repository we will get notified.
- Let me select individual events: We can configure for what events we want our data.
Click Create and a webhook will be created.
Here 34.72.39.165:8080 is the Ip and port where my jenkins is running.
Note: Github webook is able to configure for localhost jenkins.
So By configuring this whenever anyone commit or push into your repository it will directly trigger your multibranch pipeline automatically.
Referances:
- https://www.jenkins.io/doc/book/pipeline/multibranch/
- https://requestbin.com/docs/use-cases/github/?gclid=EAIaIQobChMIyoGf_fT36wIVYdxMAh2ZigQREAAYASAAEgLOufD_BwE
If you want to create a simple jenkins pipeline you can check out this
