Create a function triggered by a GitHub webhook

Learn how to create a function that is triggered by an HTTP webhook request with a GitHub-specific payload.

Github Webhook triggered function in the Azure portal

Prerequisites

  • A GitHub account with at least one project.
  • An Azure subscription. If you don't have one, create a free account before you begin.

Favorite Functions in the portal

If you haven't already done so, add Function Apps to your favorites in the Azure portal. This makes it easier to find your function apps. If you have already done this, skip to the next section.

  1. Log in to the Azure portal.

  2. Click the arrow at the bottom left to expand all services, type Functions in the Filter field, and then click the star next to Function Apps.

    Create function app in the Azure portal

    This adds the Functions icon to the menu on the left of the portal.

  3. Close the menu, then scroll down to the bottom to see the Functions icon. Click this icon to see a list of all your function apps. Click your function app to work with functions in this app.

    Function Apps in Favorites

Create an Azure Function app

  1. Click Create a resource in the upper left-hand corner of the Azure portal, then select Compute > Function App.

    Create a function app in the Azure portal

  2. Use the function app settings as specified in the table below the image.

    Define new function app settings

    Setting Suggested value Description
    App name Globally unique name Name that identifies your new function app. Valid characters are a-z, 0-9, and -.
    Subscription Your subscription The subscription under which this new function app is created.
    Resource Group myResourceGroup Name for the new resource group in which to create your function app.
    OS Windows Serverless hosting is currently only available when running on Windows. For Linux hosting, see Create your first function running on Linux using the Azure CLI.
    Hosting plan Consumption plan Hosting plan that defines how resources are allocated to your function app. In the default Consumption Plan, resources are added dynamically as required by your functions. In this serverless hosting, you only pay for the time your functions run.
    Location West Europe Choose a region near you or near other services your functions access.
    Storage account Globally unique name Name of the new storage account used by your function app. Storage account names must be between 3 and 24 characters in length and may contain numbers and lowercase letters only. You can also use an existing account.
  3. Click Create to provision and deploy the new function app. You can monitor the status of the deployment by clicking the Notification icon in the upper-right corner of the portal.

    Define new function app settings

    Clicking Go to resource takes you to your new function app.

Function app successfully created.

Next, you create a function in the new function app.

Create a GitHub webhook triggered function

  1. Expand your function app and click the + button next to Functions. If this is the first function in your function app, select Custom function. This displays the complete set of function templates.

    Functions quickstart page in the Azure portal

  2. In the search field, type github and then choose your desired language for the GitHub webhook trigger template.

    Choose the GitHub webhook trigger template

  3. Type a Name for your function, then select Create.

    Configure the GitHub webhook triggered function in the Azure portal

  4. In your new function, click </> Get function URL, then copy and save the values. Do the same thing for </> Get GitHub secret. You use these values to configure the webhook in GitHub.

    Review the function code

Next, you create a webhook in your GitHub repository.

Configure the webhook

  1. In GitHub, navigate to a repository that you own. You can also use any repository that you have forked. If you need to fork a repository, use https://github.com/Azure-Samples/functions-quickstart.

  2. Click Settings, then click Webhooks, and Add webhook.

    Add a GitHub webhook

  3. Use settings as specified in the table, then click Add webhook.

    Set the webhook URL and secret

Setting Suggested value Description
Payload URL Copied value Use the value returned by </> Get function URL.
Secret Copied value Use the value returned by </> Get GitHub secret.
Content type application/json The function expects a JSON payload.
Event triggers Let me select individual events We only want to trigger on issue comment events.
Issue comment

Now, the webhook is configured to trigger your function when a new issue comment is added.

Test the function

  1. In your GitHub repository, open the Issues tab in a new browser window.

  2. In the new window, click New Issue, type a title, and then click Submit new issue.

  3. In the issue, type a comment and click Comment.

    Add a GitHub issue comment.

  4. Go back to the portal and view the logs. You should see a trace entry with the new comment text.

    View the comment text in the logs.

Clean up resources

Other quick starts in this collection build upon this quick start. If you plan to work with subsequent quick starts, tutorials, or with any of the services you have created in this quick start, do not clean up the resources.

Resources in Azure refers to function apps, functions, storage accounts, and so forth. They are grouped into resource groups, and you can delete everything in a group by deleting the group.

You created resources to complete these quickstarts. You may be billed for these resources, depending on your account status and service pricing. If you don't need the resources anymore, here's how to delete them:

  1. In the Azure portal, go to the Resource group page.

    To get to that page from the function app page, select the Overview tab and then select the link under Resource group.

    Select the resource group to delete from the function app page.

    To get to that page from the dashboard, select Resource groups, and then select the resource group that you used for this quickstart.

  2. In the Resource group page, review the list of included resources, and verify that they are the ones you want to delete.

  3. Select Delete resource group, and follow the instructions.

    Deletion may take a couple of minutes. When it's done, a notification appears for a few seconds. You can also select the bell icon at the top of the page to view the notification.

Next steps

You have created a function that runs when a request is received from a GitHub webhook.

Learn how to create functions with other kinds of triggers or how to integrate functions with other Azure services.

For more information about webhook triggers, see Azure Functions HTTP and webhook bindings.