Azure Functions 中的部署技术Deployment technologies in Azure Functions

可以使用多种不同的技术将 Azure Functions 项目代码部署到 Azure。You can use a few different technologies to deploy your Azure Functions project code to Azure. 本文提供这些技术的详尽列表,介绍哪种技术适用于哪种 Functions 风格,解释使用每种方法时会发生什么情况,并提供有关在各种场合下使用的最佳方法的建议。This article provides an exhaustive list of those technologies, describes which technologies are available for which flavors of Functions, explains what happens when you use each method, and provides recommendations for the best method to use in various scenarios. 为部署到 Azure Functions 提供支持的各种工具已根据其环境和适当的技术进行优化。The various tools that support deploying to Azure Functions are tuned to the right technology based on their context. 通常情况下,压缩部署是建议用于 Azure Functions 的部署技术。In general, zip deployment is the recommended deployment technology for Azure Functions.

部署技术的可用性Deployment technology availability

Azure Functions 支持跨平台的本地开发以及使用 Windows 和 Linux 作为托管位置。Azure Functions supports cross-platform local development and hosting on Windows and Linux. 目前,可以使用三种托管计划:Currently, three hosting plans are available:

每种计划有不同的行为。Each plan has different behaviors. 并非所有部署技术都适用于每种风格的 Azure Functions。Not all deployment technologies are available for each flavor of Azure Functions. 以下图表显示了哪些部署技术适用于操作系统和托管计划的每种组合:The following chart shows which deployment technologies are supported for each combination of operating system and hosting plan:

部署技术Deployment technology Windows 消耗计划Windows Consumption Windows 专用计划Windows Dedicated Linux 消耗计划Linux Consumption Linux 专用计划Linux Dedicated
外部包 URL1External package URL1
压缩部署Zip deploy
Docker 容器Docker container
Web 部署Web Deploy
源代码管理Source control
本地 Git1Local Git1
云同步1Cloud sync1
FTP1FTP1
门户编辑Portal editing 22

1 需要手动触发器同步的部署技术。1 Deployment technology that requires manual trigger syncing.
2 仅对 Linux 上使用专用计划的 Functions 的 HTTP 和计时器触发器启用门户编辑。2 Portal editing is enabled only for HTTP and Timer triggers for Functions on Linux using dedicated plans.

关键概念Key concepts

若要了解 Azure Functions 中的部署工作原理,必须先掌握一些关键概念。Some key concepts are critical to understanding how deployments work in Azure Functions.

触发器同步Trigger syncing

更改任何触发器时,Functions 基础结构必须意识到这些更改。When you change any of your triggers, the Functions infrastructure must be aware of the changes. 对于许多部署技术而言,同步会自动进行。Synchronization happens automatically for many deployment technologies. 但在某些情况下,必须手动同步触发器。However, in some cases, you must manually sync your triggers. 通过引用外部包 URL、本地 Git、云同步或 FTP 部署更新时,必须手动同步触发器。When you deploy your updates by referencing an external package URL, local Git, cloud sync, or FTP, you must manually sync your triggers. 可通过以下三种方式之一来同步触发器:You can sync triggers in one of three ways:

  • 在 Azure 门户中重启函数应用Restart your function app in the Azure portal
  • 使用主密钥将 HTTP POST 请求发送到 https://{functionappname}.chinacloudsites.cn/admin/host/synctriggers?code=<API_KEY>Send an HTTP POST request to https://{functionappname}.chinacloudsites.cn/admin/host/synctriggers?code=<API_KEY> using the master key.
  • 将 HTTP POST 请求发送到 https://management.chinacloudapi.cn/subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP_NAME>/providers/Microsoft.Web/sites/<FUNCTION_APP_NAME>/syncfunctiontriggers?api-version=2016-08-01Send an HTTP POST request to https://management.chinacloudapi.cn/subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP_NAME>/providers/Microsoft.Web/sites/<FUNCTION_APP_NAME>/syncfunctiontriggers?api-version=2016-08-01. 请将占位符替换为你的订阅 ID、资源组名称和函数应用名称。Replace the placeholders with your subscription ID, resource group name, and the name of your function app.

远程生成Remote build

Azure Functions 可以自动在它在压缩部署后接收的代码上执行生成操作。Azure Functions can automatically perform builds on the code it receives after zip deployments. 这些生成的行为存在轻微差异,具体取决于应用是在 Windows 上还是在 Linux 上运行。These builds behave slightly differently depending on whether your app is running on Windows or Linux. 如果此前已将应用设置为在从包运行模式下运行,则不会执行远程生成。Remote builds are not performed when an app has previously been set to run in Run From Package mode. 若要了解如何使用远程生成,请导航到 zip 部署To learn how to use remote build, navigate to zip deploy.

Note

如果存在远程生成问题,可能是因为应用是在该功能发布日期(2019 年 8 月 1 日)之前创建的。If you're having issues with remote build, it might be because your app was created before the feature was made available (August 1, 2019). 尝试创建新的函数应用,或运行 az functionapp update -g <RESOURCE_GROUP_NAME> -n <APP_NAME> 更新函数应用。Try creating a new function app, or running az functionapp update -g <RESOURCE_GROUP_NAME> -n <APP_NAME> to update your function app. 此命令可能需要尝试两次才能成功。This command might take two tries to succeed.

Windows 上的远程生成Remote build on Windows

在 Windows 上运行的所有函数应用都有一个小的管理应用,即 SCM(或 Kudu)站点。All function apps running on Windows have a small management app, the SCM (or Kudu) site. 该站点会处理 Azure Functions 的很多部署和生成逻辑。This site handles much of the deployment and build logic for Azure Functions.

将应用部署到 Windows 时,会运行特定于语言的命令,例如 dotnet restore (C#) 或 npm install (JavaScript)。When an app is deployed to Windows, language-specific commands, like dotnet restore (C#) or npm install (JavaScript) are run.

Linux 上的远程生成Remote build on Linux

若要在 Linux 上启用远程生成,必须设置以下应用程序设置To enable remote build on Linux, the following application settings must be set:

  • ENABLE_ORYX_BUILD=true
  • SCM_DO_BUILD_DURING_DEPLOYMENT=true

默认情况下,Azure Functions Core Tools适用于 Visual Studio Code 的 Azure Functions 扩展在部署到 Linux 时执行远程生成。By default, both Azure Functions Core Tools and the Azure Functions Extension for Visual Studio Code perform remote builds when deploying to Linux. 因此,这两种工具在 Azure 中自动创建这些设置。Because of this, both tools automatically create these settings for you in Azure.

在 Linux 上以远程方式生成应用时,应用从部署包运行When apps are built remotely on Linux, they run from the deployment package.

消耗计划Consumption plan

在消耗计划中运行的 Linux 函数应用没有 SCM/Kudu 站点,这限制了部署选项。Linux function apps running in the Consumption plan don't have an SCM/Kudu site, which limits the deployment options. 但是,在消耗计划中运行的 Linux 上的函数应用支持远程生成。However, function apps on Linux running in the Consumption plan do support remote builds.

专用计划Dedicated plans

专用(应用服务)计划中的 Linux 上运行的函数应用也具有受限的 SCM/Kudu 站点。Function apps running on Linux in the Dedicated (App Service) plan also have a limited SCM/Kudu site.

部署技术详细信息Deployment technology details

Azure Functions 中提供了以下部署方法。The following deployment methods are available in Azure Functions.

外部包 URLExternal package URL

可以使用外部包 URL 来引用包含函数应用的远程包 (.zip) 文件。You can use an external package URL to reference a remote package (.zip) file that contains your function app. 可从提供的 URL 下载该文件,应用将在“从包运行”模式下运行。The file is downloaded from the provided URL, and the app runs in Run From Package mode.

如何使用:WEBSITE_RUN_FROM_PACKAGE 添加到应用程序设置。How to use it: Add WEBSITE_RUN_FROM_PACKAGE to your application settings. 此设置的值应是一个 URL(要运行的特定包文件的位置)。The value of this setting should be a URL (the location of the specific package file you want to run). 可以在门户中使用 Azure CLI 来添加设置。You can add settings either in the portal or by using the Azure CLI.

如果使用 Azure Blob 存储,请结合共享访问签名 (SAS) 使用专用容器,使 Functions 能够访问该包。If you use Azure Blob storage, use a private container with a shared access signature (SAS) to give Functions access to the package. 每当应用程序重启时,都会提取内容的副本。Any time the application restarts, it fetches a copy of the content. 引用必须在应用程序的整个生存期内有效。Your reference must be valid for the lifetime of the application.

何时使用: 如果用户不希望进行远程生成,则对于消耗计划中的在 Linux 上运行的 Azure Functions,外部包 URL 是唯一受支持的部署方法。When to use it: External package URL is the only supported deployment method for Azure Functions running on Linux in the Consumption plan, if the user doesn't want a remote build to occur. 更新函数应用引用的包文件时,必须手动同步触发器,以告知 Azure 你的应用程序已更改。When you update the package file that a function app references, you must manually sync triggers to tell Azure that your application has changed.

压缩部署Zip deploy

使用压缩部署可将包含函数应用的 .zip 文件推送到 Azure。Use zip deploy to push a .zip file that contains your function app to Azure. 可以选择将应用设置为开始从包运行,或者指定进行远程生成Optionally, you can set your app to start running from package, or specify that a remote build occurs.

如何使用: 使用偏爱的客户端工具进行部署:Visual Studio CodeVisual StudioAzure Functions Core ToolsAzure CLIHow to use it: Deploy by using your favorite client tool: Visual Studio Code, Visual Studio, the Azure Functions Core Tools, or the Azure CLI. 默认情况下,这些工具使用 zip 部署并从包运行By default, these tools use zip deployment and run from package. Core Tools 和 Visual Studio Code 扩展在部署到 Linux 时都启用远程生成Core Tools and the Visual Studio Code extension both enable remote build when deploying to Linux. 若要手动将 .zip 文件部署到函数应用,请遵照从 .zip 文件或 URL 进行部署中的说明操作。To manually deploy a .zip file to your function app, follow the instructions in Deploy from a .zip file or URL.

使用压缩部署方法时,可将应用设置为从包运行When you deploy by using zip deploy, you can set your app to run from package. 若要从包运行,请将 WEBSITE_RUN_FROM_PACKAGE 应用程序设置值设置为 1To run from package, set the WEBSITE_RUN_FROM_PACKAGE application setting value to 1. 我们建议使用压缩部署。We recommend zip deployment. 此方法可以缩短应用程序加载时间,并且是 VS Code、Visual Studio 和 Azure CLI 的默认部署方法。It yields faster loading times for your applications, and it's the default for VS Code, Visual Studio, and the Azure CLI.

何时使用: 压缩部署是建议用于 Azure Functions 的部署技术。When to use it: Zip deploy is the recommended deployment technology for Azure Functions.

Docker 容器Docker container

可以部署包含函数应用的 Linux 容器映像。You can deploy a Linux container image that contains your function app.

如何使用: 在专用计划中创建一个 Linux 函数应用,并指定要从哪个容器映像运行。How to use it: Create a Linux function app in the Dedicated plan and specify which container image to run from. 可通过两种方式实现此目的:You can do this in two ways:

  • 在 Azure 门户上的 Azure 应用服务计划中创建一个 Linux 函数应用。Create a Linux function app on an Azure App Service plan in the Azure portal. 对于“发布”,请选择“Docker 映像”,然后配置容器。 For Publish, select Docker Image, and then configure the container. 输入映像所在的位置。Enter the location where the image is hosted.

若要使用自定义容器部署到现有应用,请在 Azure Functions Core Tools 中使用 func deploy 命令。To deploy to an existing app by using a custom container, in Azure Functions Core Tools, use the func deploy command.

何时使用: 需要更好地控制运行函数应用的 Linux 环境时,请使用 Docker 容器选项。When to use it: Use the Docker container option when you need more control over the Linux environment where your function app runs. 此部署机制仅适用于在 Linux 上运行的 Functions。This deployment mechanism is available only for Functions running on Linux.

Web 部署 (MSDeploy)Web Deploy (MSDeploy)

Web 部署可打包 Windows 应用程序(包括 Azure 中的 Windows 上运行的函数应用)并将其部署到任何 IIS 服务器。Web Deploy packages and deploys your Windows applications to any IIS server, including your function apps running on Windows in Azure.

如何使用: 使用 Visual Studio Tools for Azure FunctionsHow to use it: Use Visual Studio tools for Azure Functions. 清除“从包文件运行(建议)”复选框。 Clear the Run from package file (recommended) check box.

还可以下载 Web 部署 3.6 并直接调用 MSDeploy.exeYou can also download Web Deploy 3.6 and call MSDeploy.exe directly.

何时使用: 可以顺利地支持 Web 部署,但是,首选机制是在启用“从包运行”的情况下使用压缩部署When to use it: Web Deploy is supported and has no issues, but the preferred mechanism is zip deploy with Run From Package enabled. 有关详细信息,请参阅 Visual Studio开发指南To learn more, see the Visual Studio development guide.

源代码管理Source control

使用源代码管理将函数应用连接到 Git 存储库。Use source control to connect your function app to a Git repository. 更新该存储库中的代码会触发部署。An update to code in that repository triggers deployment. 有关详细信息,请参阅 Kudu WikiFor more information, see the Kudu Wiki.

如何使用: 使用门户 Functions 区域中的部署中心设置从源代码管理进行发布。How to use it: Use Deployment Center in the Functions area of the portal to set up publishing from source control.

何时使用: 对于协作开发函数应用的团队而言,最佳做法是使用源代码管理。When to use it: Using source control is the best practice for teams that collaborate on their function apps. 源代码管理是一种很好的部署选项,可实现更复杂的部署管道。Source control is a good deployment option that enables more sophisticated deployment pipelines.

本地 GitLocal Git

可以使用本地 Git 将代码从本地计算机推送到 Azure Functions。You can use local Git to push code from your local machine to Azure Functions by using Git.

如何使用: 请遵照使用本地 Git 部署到 Azure 应用服务中的说明。How to use it: Follow the instructions in Local Git deployment to Azure App Service.

何时使用: 一般而言,我们建议使用其他部署方法。When to use it: In general, we recommend that you use a different deployment method. 从本地 Git 发布时,必须手动同步触发器When you publish from local Git, you must manually sync triggers.

云同步Cloud sync

使用云同步可将 Dropbox 和 OneDrive 中的内容同步到 Azure Functions。Use cloud sync to sync your content from Dropbox and OneDrive to Azure Functions.

何时使用: 一般而言,我们建议使用其他部署方法。When to use it: In general, we recommend other deployment methods. 使用云同步发布时,必须手动同步触发器When you publish by using cloud sync, you must manually sync triggers.

FTPFTP

可以使用 FTP 将文件直接传输到 Azure Functions。You can use FTP to directly transfer files to Azure Functions.

如何使用: 请遵照使用 FTP/s 部署内容中的说明。How to use it: Follow the instructions in Deploy content by using FTP/s.

何时使用: 一般而言,我们建议使用其他部署方法。When to use it: In general, we recommend other deployment methods. 使用 FTP 发布时,必须手动同步触发器When you publish by using FTP, you must manually sync triggers.

门户编辑Portal editing

在基于门户的编辑器中,可以直接编辑函数应用中的文件(基本上每次保存更改都要进行部署)。In the portal-based editor, you can directly edit the files that are in your function app (essentially deploying every time you save your changes).

如何使用: 若要在 Azure 门户中编辑函数,必须事先在门户中创建函数How to use it: To be able to edit your functions in the Azure portal, you must have created your functions in the portal. 若要保留单一事实源,使用任何其他部署方法会使函数变为只读,并会阻止在门户中继续编辑。To preserve a single source of truth, using any other deployment method makes your function read-only and prevents continued portal editing. 若要恢复到可在 Azure 门户中编辑文件的状态,可以手动将编辑模式改回 Read/Write,并删除与部署相关的任何应用程序设置(例如 WEBSITE_RUN_FROM_PACKAGE)。To return to a state in which you can edit your files in the Azure portal, you can manually turn the edit mode back to Read/Write and remove any deployment-related application settings (like WEBSITE_RUN_FROM_PACKAGE).

何时使用: 在门户中可以十分方便地开始使用 Azure Functions。When to use it: The portal is a good way to get started with Azure Functions. 对于更密集的开发工作,我们建议使用以下客户端工具之一:For more intense development work, we recommend that you use one of the following client tools:

下表显示了支持门户编辑的操作系统和语言:The following table shows the operating systems and languages that support portal editing:

Windows 消耗计划Windows Consumption Windows 专用计划Windows Dedicated Linux 消耗计划Linux Consumption Linux 专用计划Linux Dedicated
C#C#
C# 脚本C# Script **
F#F#
JavaJava
JavaScript (Node.js)JavaScript (Node.js) **
PowerShell(预览版)PowerShell (Preview)
TypeScript (Node.js)TypeScript (Node.js)

* 仅对 Linux 上使用专用计划的 Functions 的 HTTP 和计时器触发器启用门户编辑。* Portal editing is enabled only for HTTP and Timer triggers for Functions on Linux using dedicated plans.

部署槽Deployment slots

将函数应用部署到 Azure 时,可以部署到单独的部署槽,而不是直接部署到生产槽。When you deploy your function app to Azure, you can deploy to a separate deployment slot instead of directly to production. 有关部署槽的详细信息,请参阅 Azure Functions 部署槽文档。For more information on deployment slots, see the Azure Functions Deployment Slots documentation for details.

后续步骤Next steps

请阅读以下文章详细了解如何部署函数应用:Read these articles to learn more about deploying your function apps: