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. 本文概述了可用的部署方法,以及在各种方案中使用的最佳方法建议。This article provides an overview of the deployment methods available to you and recommendations for the best method to use in various scenarios. 此外,还提供了有关基本部署技术的详尽列表和重要详细信息。It also provides an exhaustive list of and key details about the underlying deployment technologies.

部署方法Deployment methods

用于将代码发布到 Azure 的部署技术通常由发布应用的方式决定。The deployment technology you use to publish code to Azure is generally determined by the way in which you publish your app. 适当的部署方法取决于特定需求和开发周期中所处的阶段。The appropriate deployment method is determined by specific needs and the point in the development cycle. 例如,在开发和测试期间,可以直接从开发工具(如 Visual Studio Code)进行部署。For example, during development and testing you may deploy directly from your development tool, such as Visual Studio Code. 当应用处于生产阶段时,你更有可能从源代码管理或使用自动发布管道(包括更多验证和测试)持续发布。When your app is in production, you are more likely to publish continuously from source control or by using an automated publishing pipeline, which includes additional validation and testing.

下表介绍了 Function 项目的可用部署方法。The following table describes the available deployment methods for your Function project.

部署类型Deployment type 方法Methods 最适用于...Best for...
基于工具Tools-based • Visual Studio Code 发布• Visual Studio Code publish
• Visual Studio 发布• Visual Studio publish
• 核心工具发布• Core Tools publish
开发期间的部署和其他临时部署。Deployments during development and other ad-hock deployments. 部署由工具在本地进行管理。Deployments are managed locally by the tooling.
外部管道External pipelines • GitHub 操作• GitHub actions 生产和 DevOps 管道,包含其他验证、测试和作为自动部署的一部分运行的其他操作。Production and DevOps pipelines that include additional validation, testing, and other actions be run as part of an automated deployment. 部署由管道进行管理。Deployments are managed by the pipeline.

虽然特定的 Functions 部署使用基于其上下文的最佳技术,但大多数部署方法都基于 zip 部署While specific Functions deployments use the best technology based on their context, most deployment methods are based on zip deployment.

部署技术的可用性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
门户编辑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}<API_KEY>Send an HTTP POST request to https://{functionappname}<API_KEY> using the master key.
  • 将 HTTP POST 请求发送到<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP_NAME>/providers/Microsoft.Web/sites/<FUNCTION_APP_NAME>/syncfunctiontriggers?api-version=2016-08-01Send an HTTP POST request to<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.


如果存在远程生成问题,可能是因为应用是在该功能发布日期(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:


默认情况下,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 Studio,或从命令行使用 Azure Functions Core ToolsHow to use it: Deploy by using your favorite client tool: Visual Studio Code, Visual Studio, or from the command line using the Azure Functions Core Tools. 默认情况下,这些工具使用 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.

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.


可以使用 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:

LanguageLanguage Windows 消耗计划Windows Consumption Windows 专用计划Windows Dedicated Linux 消耗计划Linux Consumption Linux 专用计划Linux Dedicated
C# 脚本C# Script **
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 behaviors

在进行部署时,允许所有现有执行完成或超时,然后加载新代码以开始处理请求。When you do a deployment, all existing executions are allowed to complete or time out, after which the new code is loaded to begin processing requests.

如果需要对此转换进行更多控制,则应使用部署槽。If you need more control over this transition, you should use deployment slots.

部署槽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: