Thursday, March 2, 2023

How to create webhooks in GitHub?

How to create webhooks in GitHub?


What are webhooks in GitHub and how to you use them? If you want to know how to use webhook in GitHub or how to integrate webhook in GitHub then this is right place for you. In this article I will discuss about what a repository webhook is and how to use them.

If you haven’t seen my previous article on webhook then I strongly recommend to read that first and then come here. In this article we will try to demonstrate a webhook in GitHub with the help of Azure Function. Azure Function can be used to respond to events that occur in your GitHub repositories.

As we already know that, a webhook is an HTTP-based callback function which allows external services to be notified when certain events happen. When the specified events happen, webhook will send a POST request to each of the URLs we provide to him.

So, in this demo to create this callback function we will use Azure Function. It will provide us an HTTP URL which we will configure in GitHub webhook. After that we will make some setting in GitHub for execution of the webhook on an event. Then finally we will do some action in GitHub to perform that event and will see the result either webhook is being called or not.

Lets first I’m creating an Azure Function app in Azure portal. Here, I’m not going in details how to create Azure Function app. I will create a separate article on this.

For this demo, I create a function app “myFunAppTest” and within this app then I created a function “HttpTrigger1”.

This function contains some default code which runs when this function gets executed. In this function we can write code as per our requirements. For the demo purpose, I’m keeping it default.

On the same screen, on top right corner there is a link “Get function URL”.  Just click on it and you will get the HTTP-based URL to call this function. Copy this URL as later on we will configure it on GitHub webhook.


Now let’s come to GitHub. For the demo purpose I created a test repository “TestRepository” which contains a text file “Customer.txt”. What we want to achieve here is that if there will be any change in this text file then webhook should call the above Azure Function.


Now go to “Settings” tab of this “TestRepository” and clicks on “Webhooks” link. Now click on “Add webhook” button.


Now under the “Payload URL” text box, paste the Azure Function URL and select the "Content Type" as "application/json". After that select the “Send me everything” radio button and click on “Add webhook” button.

Once the webhook will be add successfully then Webhooks screen will look like as below and will display newly added webhook.


We are done with all configurations. Now, we just need to test it. For that we will go to repository text file and will make some changes in that and will commit our changes.

Once it will be done successfully, we can see from logs that Azure Function being called by GitHub webhook.


If you have any queries/suggestions or want to ask any questions you can comment below or can email me on my email id.

Thank you for reading this article and by giving your time.


No comments:

Post a Comment

Will it be possible to live on Mars, as Elon Musk has said?

Elon Musk has expressed his ambition to establish a human presence on Mars in the future. While living on Mars is a long-term goal for Space...