Before starting setting up webhook lets first see what actually Github webhook is.
A github webhook aims at providing realtime data to other applications whenever any set of events occur.It is also referred to as a Web Callback often. A webhook is fast because it does it right away. There are a set of instructions specified in github for whatever event you want to take it.
For example :
1- It can be a pull request
2 – An event of commit can also trigger the webhook
3 – A push event and many more.
We need webhook in CI tools , notification tools. A very good example that uses these webhook is Jenkins. Its common use to use webhook in jenkins on pull and push event to trigger the pipeline.
But what if Jenkins is hosted on Localhost ?
Its common to use github webook when jenkins is hosted on any cloud. But if Jenkins is runnnig on localhost then the webhook doesnt work.So for such cases ngrok comes into play.
What is ngrok ?
ngrok can be said as a cross-platform application that can expose our localhost to internet.It hosts the local development server on its subdomain ngrok.com. So we dont need any public ip on this.
To download ngrok you can visit the link https://ngrok.com/download. After downloading follow the below steps for setting up the tunnel.
1 – Download and extract from the above link
2- Start a tunnel with the command below
ngrok <protocol> <port>
ngrok http 8080
or you can run from the executable by below steps.
1- Move into the directory of ngrok in downloaded folder.
2- Then start the tunnel by
rishivant@rishivant-Vostro-3590:~/Downloads/ngrok-stable-linux-amd64$ ls
ngrok
rishivant@rishivant-Vostro-3590:~/Downloads/ngrok-stable-linux-amd64$ ./ngrok http 80
Output:-
In the above image you can see forwarding and that is the where your localhost is exposed to.
Setting up Webhook using ngrok
Follow the below steps :
1 – Login into your github.
2- Go to your repository setting
3- Move to the webhook sesction
4- Click on Add webhook on the above right corner
5 – Then in the payload url put your ngrok exposed url like below
http://0f20-122-163-123-169.ngrok.io/github-webhook/
Now comes the main part i.e events.It depends on you like for which event you want your pipeline in Jenkins to get triggered.
6- Go to Let me select individual event .There you will get many option and select as per your need.
Mostly the event selected is
a – Pushes – In this case the pipeline will be triggered on any push in repository.
b – Pull request – Whenever there is any PR generated to the branch then this will trigger the pipeline in Jenkins to t triggered.
Select these two events and then you are good to go with the webhooks.
Now whenever there will be a push or pull request on you repository Jenkins will automatically start building for the PR generated. Then you pipeline will be triggered and it will be build successfully.
Use Case-:
1 – This is helpful when you build any project locally on your own system.
2 – When a project is built locally but you want to trigger the pipeline automatically then this helps .
Conclusion
So this is the best way if you want to use github webhook when running your jenkins or any tool on localhost.This way can be most productive when we deal with local server build and deploy.It is a god practise and can be achieved in a second . To apply this follow the above steps as it is.
References.
1 – https://hevodata.com/learn/github-webhook/