Create a function app in Azure that is deployed from GitHub

This Azure Functions sample script creates a function app using the Consumption plan, along with its related resources. The script also configures your function code for continuous deployment from a GitHub repository.

In this sample, you need:

  • A GitHub repository with functions code, that you have administrative permissions for.
  • A Personal Access Token (PAT) for your GitHub account.

If you don't have an Azure subscription, create a free account before you begin.

Prerequisites

  • Use Azure Cloud Shell using the Bash environment.

    Launch Cloud Shell in a new window

  • If you prefer, install the Azure CLI to run CLI reference commands.

    • If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For additional sign-in options, see Sign in with the Azure CLI.
    • When you're prompted, install Azure CLI extensions on first use. For more information about extensions, see Use extensions with the Azure CLI.
    • Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.
  • This tutorial requires version 2.0 or later of the Azure CLI. If using Azure Cloud Shell, the latest version is already installed.

Sample script

This sample creates an Azure Function app and deploys function code from GitHub.

#!/bin/bash

# Function app and storage account names must be unique.
storageName=mystorageaccount$RANDOM
functionAppName=mygithubfunc$RANDOM
region=westeurope

# TODO:
gitrepo=<Replace with your GitHub repo URL e.g. https://github.com/Azure-Samples/functions-quickstart.git>
token=<Replace with a GitHub access token>

# Enable authenticated git deployment in your subscription from a private repo.
az functionapp deployment source update-token \
  --git-token $token

# Create a resource group.
az group create \
  --name myResourceGroup \
  --location $region

# Create an Azure storage account in the resource group.
az storage account create \
  --name $storageName \
  --location $region \
  --resource-group myResourceGroup \
  --sku Standard_LRS

# Create a function app with source files deployed from the specified GitHub repo.
az functionapp create \
  --name $functionAppName \
  --storage-account $storageName \
  --consumption-plan-location $region \
  --resource-group myResourceGroup \
  --deployment-source-url $gitrepo \
  --deployment-source-branch master \
  --functions-version 2

Clean up deployment

After the sample script has been run, the following command can be used to remove the resource group and all resources associated with it.

az group delete --name myResourceGroup

Script explanation

Each command in the table links to command specific documentation. This script uses the following commands:

Command Notes
az group create Creates a resource group in which all resources are stored.
az storage account create Creates the storage account required by the function app.
az functionapp create Creates a function app in the serverless Consumption plan and associates it with a Git or Mercurial repository.

Next steps

For more information on the Azure CLI, see Azure CLI documentation.

Additional Azure Functions CLI script samples can be found in the Azure Functions documentation.