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

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 Premium (preview) Windows 专用计划Windows Dedicated Linux 消耗计划Linux Consumption Linux 高级版 (预览)Linux Premium (preview) Linux 专用计划Linux Dedicated
外部包 URL1External package URL1
压缩部署Zip deploy
Docker 容器Docker container
Web 部署Web Deploy
源代码管理Source control
本地 Git1Local Git1
云同步1Cloud sync1
FTP1FTP1
门户编辑Portal editing 22 22

1 需要手动触发器同步的部署技术。1 Deployment technology that requires manual trigger syncing.
2仅对 Linux 上使用高级和专用计划的 HTTP 和计时器触发器启用门户编辑。2 Portal editing is enabled only for HTTP and Timer triggers for Functions on Linux using Premium and 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}.azurewebsites.net/admin/host/synctriggers?code=<API_KEY>Send an HTTP POST request to https://{functionappname}.azurewebsites.net/admin/host/synctriggers?code=<API_KEY> using the master key.
  • 将 HTTP POST 请求发送到 https://management.azure.com/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.azure.com/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 可以自动对其在实现 zip 部署后收到的代码执行生成。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.

备注

如果你在远程生成时遇到问题, 可能是因为你的应用程序是在提供该功能之前创建的 (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). 尝试创建新的 function app。Try creating a new function app.

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 (preview)

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

  • ENABLE_ORYX_BUILD=true
  • SCM_DO_BUILD_DURING_DEPLOYMENT=true

如果应用是在 Linux 上远程构建的, 则它们从部署包运行When apps are built remotely on Linux, they run from the deployment package.

备注

目前只有 node.js 和 Python 支持 Linux 专用 (应用服务) 计划的远程构建。Remote build on the Linux Dedicated (App Service) plan is currently only supported for Node.js and Python.

使用情况 (预览) 计划Consumption (preview) plan

在消耗计划中运行的 Linux function apps 没有 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. 这些远程生成使用OryxThese remote builds use Oryx.

专用和高级 (预览版) 计划Dedicated and Premium (preview) plans

在 Linux 上运行的适用于专用 (应用服务) 计划高级计划的函数应用还具有受限的 SCM/Kudu 站点, 该站点自身利用了OryxFunction apps running on Linux in the Dedicated (App Service) plan and the Premium plan also have a limited SCM/Kudu site, which itself takes advantage of Oryx.

部署技术详细信息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 specifically 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.

如何使用: 使用偏爱的客户端工具进行部署:VS CodeVisual StudioAzure CLIHow to use it: Deploy by using your favorite client tool: VS Code, Visual Studio, or the Azure CLI. 若要手动将 .zip 文件部署到函数应用,请遵照从 .zip 文件或 URL 进行部署中的说明操作。To manually deploy a .zip file to your function app, follow the instructions in Deploy from a .zip file or URL.

若要执行使用远程生成的 zip 部署, 请使用以下Core 工具命令:To perform a zip deploy with a remote build, use the following Core Tools command:

func azure functionapp publish <app name> --build remote

或者, 你可以通过添加 "azureFunctions. scmDoBuildDuringDeployment" 标志, 指示 VS Code 在部署时执行远程生成。Alternatively, you can instruct VS Code to perform a remote build when deploying by adding the ``azureFunctions.scmDoBuildDuringDeployment" flag. 若要了解如何将标志添加到 VS Code, 请参阅Azure Functions 扩展 Wiki中的说明。To learn how to add a flag to VS Code, read the instructions in the Azure Functions Extension Wiki.

使用 zip 部署进行部署时, 可以将应用设置为从包中运行When you deploy by using zip deploy, you can set your app to run from package. 若要从包中运行, WEBSITE_RUN_FROM_PACKAGE请将应用程序1设置值设置为。To 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.

何时使用: Zip 部署是 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 Premium or 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 CLI 在应用服务计划中创建 Linux 函数应用。Create a Linux function app on an App Service plan by using the Azure CLI. 若要了解如何操作, 请参阅在 Linux 上使用自定义映像创建函数To learn how, see Create a function on Linux by using a custom image.

若要使用自定义容器部署到现有应用,请在 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 上运行的函数。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.

如何使用: 使用门户的 "功能" 区域中的 "部署中心" 设置从源代码管理发布。How to use it: Use Deployment Center in the Functions area of the portal to set up publishing from source control. 有关详细信息,请参阅 Azure Functions 的连续部署For more information, see Continuous deployment for Azure Functions.

何时使用: 对于协作开发函数应用的团队而言,最佳做法是使用源代码管理。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.

如何使用: 按照同步云文件夹中的内容中的说明进行操作。How to use it: Follow the instructions in Sync content from a cloud folder.

何时使用: 一般而言,我们建议使用其他部署方法。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 Premium (preview) Windows 专用计划Windows Dedicated Linux 消耗计划Linux Consumption Linux 高级版 (预览)Linux Premium (preview) Linux 专用计划Linux Dedicated
C#C#
C# 脚本C# Script ** **
F#F#
JavaJava
JavaScript (Node.js)JavaScript (Node.js) ** **
Python(预览版)Python (Preview)
PowerShell(预览版)PowerShell (Preview)
TypeScript (Node.js)TypeScript (Node.js)

*仅对 Linux 上使用高级和专用计划的 HTTP 和计时器触发器启用门户编辑。* Portal editing is enabled only for HTTP and Timer triggers for Functions on Linux using Premium and 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: