In this article, I will list out steps for a popular/mandatory automation in DevOps world. Let’s see how you can setup Jenkins job to trigger on every push to a specific git branch
Triggering a Jenkins job for every Git push is needed if you are planning to setup automated release of applications. Techies have been using Poll SCM for a long time, but it is proved that Poll SCM is costly and inefficient when you have plenty of jobs and git repo is huge. Let’s say “bye bye” to Poll SCM and shake your hands with Webhooks . Yeah, you can setup git webhook to trigger Jenkins job whenever there is a change in Git repo.
Step 1: Install Git plugin in Jenkins
Jenkins doesn’t have builtin functionality to do our job. But there is a Git plugin which does this for us. You might have it already installed as we generally use this plugin to have Git as a build SCM. If you don’t, install Git plugin by following this article. You don’t necessarily need to tweak global settings of this plugin for this task.
Step 2: Configure Jenkins Job to trigger on Push
Now just go to your Jenkins job and Click Configure button.
Under Source Code Management section, either select Git or Multiple SCMs . Then fill up your Git repo URL and Branches to Build. Note that, Jenkins job will only trigger for changes made to the branch you specify here and you need to specify the branch name in refs/heads/<branch-name> syntax.
Note: Here I am telling Jenkins to look for changes in Git branch testbranch only.
We are not done yet, scroll down to Build Triggers section and check box for “
Now we are done configuring at Jenkins side. Let’s go to Git side and configure a Webhook. Also explore Git plugin here >>
Step 3: Create Webhook to send Push events to Jenkins
We have configured Jenkins to trigger a build when there is a Git repo change. Now who would notify Jenkins about the changes in Git repo? for that we need to configure a Webhook at Github. A webhook is something which listens for certain events in Git repo and whenever those events happen, a HTTP POST call is made to a Payload URL. Here the payload URL should be <Jenkins-urls>/github-webhook/ as it is associated with Git plugin. Let’s configure the webhook now.
Open your GitHub repo on the browser and under Settings , click on Hooks .
Now click on Add webhook , then fill <Jenkins-URL>/github-webhook/ as Payload URL and select Just the push event option. Then click Add webhook to save your webhook.
Once you add the webhook, check the Webhooks section to see whether your hook got a green tick.
If there is no green tick, means some issue with your webhook. You need to fix it to proceed further.
Now try pushing some changes to your Git branch and see which triggers your Jenkins build or not.
Thanks for the time taken to read my blog. Subscribe to this blog so that you don’t miss out anything useful (Checkout Right Sidebar for the Subscription Form and Facebook follow button) . Please also put your thoughts as comments .