Github Webhooks to Trigger Jenkins Pipeline
Trigger the execution of Jenkins jobs based on GitHub events
Github provides a handy way to call webhooks’ URLs when certain events are triggered. The list of these events is long and includes: on pushing to a repository, on a pull request opening, closing, and reopening, on creation of a release, and many more.
To make it simple, Github will perform a POST request with a payload object containing data about the triggered event to your defined webhook URL once the event is triggered. This will allow us to automate the executions of some actions based on the Github events.
For example, if after each push to the master branch we want to deploy our software, we can simply add a webhook to the Jenkins task responsible for packaging or deploying the service.
Create Pipeline project in Jenkins
Just like last blog; we are creating a pipeline job.
In the Configuration -> Bluild Triggers -> check on ''Github hook trigger for GITScm polling" j
In the advanced pipeline setting Define - Definition , SCM, Repo
Scroll down and Define - Branch Specifier, Script path
Your Jenkins Pipeline configuration is done!
Adding Jenkins Webhook in Github
Follow me blindly from now! Let me reveal some magic!
Login to GitHub -> Go to Repo (you can fork it https://github.com/kshitijabartakke/calculategrowth )-> Click on Settings
Click on Webhook
Click on Add webhook button
Fill in the details, as shown in the image below. For the payload URL, provide your Jenkins URL and the GitHub webhook path at the end of the URL — https://${jenkins_url}/github-webhook/
. You can disable SSL verifications if you don’t have a valid SSL cert for your Jenkins server. Finally, choose the option “Calling the webhook only for push events.” (This actually depends on what you are trying to do. You can change this by selecting other options).
zoom the below picture to see webhook details.
NOW, Let the magic begin!
You can edit any file, suppose I'm editing the below content to commit. (When webhook will run jenkins pipeline, on browsing http://Instance_Public_IP.118:8501/ my website will show Let us Calculate % Growth Effortlessly!
Add your commit message and hit commit changes
Now keep both the windows github and jenkins together
Build has started by itself! But who will trust??
I have something to show you in below console output
It says, Started by Gitub push by kshitijabartakke
Bonus Tip*
I have faced some challenges and would like to share one of the incidents where I lost my patience as my pipeline was unable to trigger by itself when I made commits in GitHub repository.
I found the solution and it fixed everything!
For some reason my AWS instance was unable to work properly and I stopped and started my instance manually.
Here, when stop start done my intsance public IP got changed. My Github and Jenkins connection broke. Then I went to Github webhook settings and edited webhook URL.
After making changes connection got re-established and my jenkins job run after making commits in the github.