Introduction

Azure Functions and webhooks have made Azure development reach new heights. In this article we will see how we can create an Azure Function that gets called via a webhook registered with Github events.

Create Azure Function

As the first step lets create an Azure function that will contain the logic to be executed in response to an event such as Github pull request, issue comment etc:

Specify the Azure App name and other details and create the Azure Function App.

Thus we have successfully created an Azure Function App.

The Azure Function App named ‘GithubAzureWebhook’ will be listed along with other Azure Function Apps.

Lets add a new function to the app. Click on the Plus sign to add a new function.

Select ‘Custom Function’ option from the below page.

We will select Github Webhook C# template to create the function.

By default it will contain the below code block. We can add the required functionality to the run.csx file so that it gets executed as per the requirement.

Once we are done with the addition of the logic to the function, get the function URL as shown below. We will be using this URL to register it with the Github Webhook.

Similarly get the Github Secret which will be used along with the Function URL during webhook registration.



Create Github Webhook

Now we have to create a Github webhook that will call the azure function in response to specific events happening in Github repository. Currently I have a repository created in Github under the name ‘Retrieve ListeItems Using SPFx’ .  To create the webhook. Head over to the settings tab.

Select the Webhooks options to create a new webhook.

From the Webhooks page, select ‘Add webhook’ option.

In the resulting page, add the Azure Function URL and the Secret which we had copied earlier. Set the Content Type to ‘application/json’.

In the same page, we will set the events to which the webhook should respond to. Currently we have selected Pull requests, Issue Comments related events.

Thus we have created the webhook in Github and registered the Azure function App with the webhook.

Test the webhook

Now we will test the functioning of the webhook. Lets create a new issue comment as shown below. Since we have registered Issue Comment Event with the webhook. The webhook should get triggered and it will run the Azure Function.

Upon clicking ‘Comment’ button if has triggered the webhook successfully as indicated by the tickmark shown below.

The webhook runtime history  and errors can be viewed from the webhook tab in the github repository.

Now lets head over to the Azure portal and check if the Azure function has run. From the monitor tab we can see the recent runs. The latest run is shown on top.

On Selecting the first run time and checking the log, we can see that it has captured the issue comment that was input in the github repository indicating the successfully Webhook – Azure function connectivity.



Summary

Thus we saw how to create an Azure Function that gets called via a webhook registered with Github events.