您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

在 Azure 中创建从 GitHub 部署的函数应用Create a function app in Azure that is deployed from GitHub

此 Azure Functions 示例脚本使用消耗计划和其他相关资源创建函数应用。This Azure Functions sample script creates a function app using the consumption plan, along with its related resources. 此脚本还配置函数代码,以便持续地从 GitHub 存储库进行部署。The script also configures your function code for continuous deployment from a GitHub repository.

通过此脚本创建的函数应用使用 1.x 版 Azure Functions 运行时The function app created by this script uses version 1.x of the Azure Functions runtime. 若要了解如何升级到 2.x 版,请参阅使用 Azure CLI 指定版本To learn how to upgrade to version 2.x, see Target a version using Azure CLI.

在此示例中,需要以下项:In this sample, you need:

  • 包含函数代码且你对其拥有管理权限的 GitHub 存储库。A GitHub repository with functions code, that you have administrative permissions for.
  • GitHub 帐户的个人访问令牌(PAT)A Personal Access Token (PAT) for your GitHub account.

如果还没有 Azure 订阅,可以在开始前创建一个免费帐户If you don't have an Azure subscription, create a free account before you begin.

打开 Azure Cloud ShellOpen Azure Cloud Shell

Azure Cloud Shell 是一个托管在 Azure 中的交互式 shell 环境,可通过浏览器使用。Azure Cloud Shell is an interactive shell environment hosted in Azure and used through your browse. Azure Cloud Shell 允许你使用 bashPowerShell shell 运行各种工具来使用 Azure 服务。Azure Cloud Shell allows you to use either bash or PowerShell shells to run a variety of tools to work with Azure services. Azure Cloud Shell 预安装了一些命令,允许你运行本文的内容,而不必在本地环境中安装任何内容。Azure Cloud Shell comes pre-installed with the commands to allow you to run the content of this article without having to install anything on your local environment.

若要在 Azure Cloud Shell 上运行本文中包含的任何代码,请打开 Cloud Shell 会话,对代码块使用“复制” 按钮以复制代码,然后使用 Ctrl+Shift+V(在 Windows 和 Linux 上)或 Cmd+Shift+V(在 macOS 上)将其粘贴到 Cloud Shell 会话中。To run any code contained in this article on Azure Cloud Shell, open a Cloud Shell session, use the Copy button on a code block to copy the code, and paste it into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS. 粘贴的文本不会自动执行,因此请按 Enter 运行代码。Pasted text is not automatically executed, so press Enter to run code.

可以通过以下方式启动 Azure Cloud Shell:You can launch Azure Cloud Shell with:

选择代码块右上角的“试用”。 Select Try It in the upper-right corner of a code block. 这__不__会自动将文本复制到 Cloud Shell。This doesn't automatically copy text to Cloud Shell. Azure Cloud Shell 的“试用”示例
在浏览器中打开 shell.azure.comOpen shell.azure.com in your browser. “启动 Azure Cloud Shell”按钮Launch Azure Cloud Shell button
选择 Azure 门户右上角菜单上的“Cloud Shell” 按钮。Select the Cloud Shell button on the menu in the upper-right corner of the Azure portal. Azure 门户中的“Cloud Shell”按钮

如果想要在本地使用 Azure CLI,必须安装和使用版本 2.0 或更高版本。If you rather use the Azure CLI locally, you must install and use version 2.0 or a later version. 若要确定 Azure CLI 版本,请运行 az --versionTo determine the Azure CLI version, run az --version. 如需进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install the Azure CLI.

示例脚本Sample script

此示例创建 Azure Function app 并从 GitHub 部署函数代码。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

# 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 westeurope

# Create an Azure storage account in the resource group.
az storage account create \
  --name $storageName \
  --location westeurope \
  --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 westeurope \
  --resource-group myResourceGroup \
  --deployment-source-url $gitrepo \
  --deployment-source-branch master

清理部署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 createaz group create 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
az storage account createaz storage account create 创建函数应用所需的存储帐户。Creates the storage account required by the function app.
az functionapp createaz functionapp create 在无服务器消耗计划中创建一个函数应用并将其与一个 Git 或 Mercurial 存储库相关联。Creates a function app in the serverless consumption plan and associates it with a Git or Mercurial repository.

后续步骤Next steps

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档For more information on the Azure CLI, see Azure CLI documentation.

可以在 Azure Functions 文档中找到其他 Azure Functions CLI 脚本示例。Additional Azure Functions CLI script samples can be found in the Azure Functions documentation.