For this we need to use, Github WebHooks. Webhook is an HTTP callback, an HTTP POST call that occurs when something happens through event-notification via HTTP POST. GitHub webhooks in Jenkins are used to trigger the build whenever something is committed to the branch.
For this we need to configure both at GitHub and Jenkins. We will be creating a webhook from GitHub and later use that in Jenkins Job. So, let us first do the configurations at GitHub.
Go to your project repository -> Settings -> webhooks -> Add webhooks.
Enter Payload URL as Jenkins URL where Jenkins instance is running. In my case jenkins is running at : https://626b9g85.ngrok.io
Here is one problem you have to take care,if you are running Jenkins on localhost then https://localhost:8080/github-webhook/ will not work because Webhooks can only work when they are exposed to the internet.
In this way, we can add a webhook to our job and ensure that everytime a developer commits a code to GitHub, our build will be triggered.
I Hope this article was useful to you. You can use my GitHub repository for practicing this. Here is the link: https://github.com/sonucogni/Jenkins_Pipeline_Demo