Learn how to create an Azure function that is triggered by a webhook.
- [Narrator] Let's say we have an Azure function that we would like to trigger anytime an issue is raised in a repository on GitHub. To do that, we're going to create an Azure function based on a webhook. Let's come back to our functions, click plus, and what we need is a GitHub webhook. So we'll scroll down and here we have a GitHub webhook for C#, just what we want.
Let's give that a name, scroll down, and so we're going to call that GitHubWebhookjl1. And we'll go ahead and create it. And the code comes up to process our webhook. We're going to add a log.Info line just before return and we're going to say log.Info, open up, we're going to make this a string with interpolation and we're going to say in it, Github comment, colon, and then in braces we will put our GitHub comment, which we're going to get back from our webhook.
And we need a semicolon. Let's go ahead and save that. Now there are two pieces of information that we need, the function URL and secret. Let's open up notepad and we will stash away the function URL, which we will copy and paste into notepad. And then we're going to go ahead and get the secret, copy that, and paste that into notepad.
We'll need that in just a moment. Next step, let's go to GitHub, login, or I'm already logged in, pick a repository, let's pick pomodoro1 as an example, and we're going to click on Settings, and then webhooks. And we'll add a webhook. We now need that URL that we stashed away so let's paste that into the Payload URL, come down to Secret, get the secret that we stashed away, place that in.
Now which events do we want to have trigger this webhook? Well I'm going to click on Let me select individual events. And the event that I want is an Issue comment, this will trigger the webhook anytime an issue comment is created, edited or deleted. And that's just what I want, so I'm going to go ahead and add that webhook. We now have a webhook that will be fired every time an issue is added or deleted and so forth. Let's go ahead and create an issue and to do that we want to click on Issues, I'll open that in a new tab.
And we'll say New Issue. I'm going to give that a title which is going to be Issue for Azure Web Function, and then in the body of the issue where the comment is I will say, "This is an issue for Azure Web Function." and I'm going to submit that new issue. Now having submitted the new issue, that's a create, we expect that webhook to talk to our Azure function and trigger that function.
So let's go back to our Azure function, pick up the URL, add in scm, so after function it's going to be dot scm dot azurewebsites, that brings us to Kudu, click on debug console command, LogFiles, Applications, Functions, function, there's our third which is GitHubWebhookjl1, the one we just created.
Click on that, there is our log file, let's download that. Open that in the folder, double click on it and it tells us the script has changed, compilation was successful, let's go back to our function, run it, let's go back to our issues, create yet another issue. This is a second issue for Azure and in the comments we'll write Notice this second issue for Azure.
Submit that issue, go back to our diagnostic console, let's refresh, hit LogFiles, Application, Functions, function, GitHub, download that log. Show it in the folder. Open it and here we see the GitHub comment. This is a comment on a GitHub issue. So in fact, what happened, the round trip is that we created an Azure function that is triggered by a webhook, we created that webhook in GitHub, and each time we add an issue it fires or triggers that Azure function.
We have seen now that you can fire an Azure function in three different ways that we've looked at. A GitHub webhook, an http trigger, and a timer trigger. Each of them are triggering our Azure functions.
Released
10/12/2017- Examining and personalizing the Azure portal
- Creating an app service
- Creating an ASP.NET application with Visual Studio
- Viewing your published app
- Creating a virtual machine
- Triggering Azure functions
Share this video
Embed this video
Video: Webhook