Running WebHook for Jenkins running on localhost

What is WebHook:

A webhook or a web callback or HTTP push API provides a way to send real time information to other applications. So, when ever a change happens in the source application, it sends this event information to other applications that were configured to listen to such events

How Git WebHook works

As you know git is a public repository (unless you have a local server configured), so in order to send these events to the target client it have to go through internet

In simple words, Git does not know the address ‘localhost’ so cannot send the information

So how to over come this?

Think!!!!!!

1..2..3..4..5

Ok, so the solution is to send these events to a public system and the target application get the information from the public system:

Actual implementation:

But github doesn’t know how to reach Jenkins and other apps.

Work Around:

Here, the github will send the WebHook data to a public server

The Jenkins host system polls for data from the public server

If any event is detected , then Jenkins system triggers the build

Steps

Navigate to : https://smee.io/:

This is our public server

Click ‘start a new channel’:

Install smee using the command:

npm install --global smee-client

Run smee to poll for events:

copy the url:

Run the below command:

Here, –path shows were to transfer the received packets. The hostname is localhost and its by default. We just have to mention the location of webhook

So, –path /github-webhook/ implies, http://localhost:8000/github-webhook/ (this is the default webhook handle for Jenkins)

Port specifies the port in which the application (Jenkins in this case ) runs.

smee -u https://smee.io/KXVW3UMxLTQ8y9 --path /github-webhook/ --port 8080

Now smee is polling for events from the smee server, and once event is received , it transfers the event to jenkins webhook handle.

Configure webhook in github:

Navigate to githubrepo>settings>webhooks>Add webhooks

Copy the url from smee and paste it in payload url

Change content type as json and make events to ‘Send everything’

Configure Jenkins:

Give git repository name as usual

Set build trigger as github hook

Build the job manually by clicking ‘build’ after creating the job.

Now try commiting new changes to your git repo. You can see that , when ever something changes in git repo, the build starts automatically.

Note: you have manually initiate build first time, after that it works automatically

Reference: https://jenkins.io/blog/2019/01/07/webhook-firewalls/