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

Azure Functions 的 Zip 部署Zip deployment for Azure Functions

本文介绍如何将函数应用项目文件从 .zip(压缩)文件部署到 Azure。This article describes how to deploy your function app project files to Azure from a .zip (compressed) file. 可在本文中了解如何通过 Azure CLI 和 REST API 进行推送部署。You learn how to do a push deployment, both by using Azure CLI and by using the REST APIs. Azure Functions 核心工具在将本地项目发布到 Azure 时也使用这些部署 API。Azure Functions Core Tools also uses these deployment APIs when publishing a local project to Azure.

Azure Functions 具有完整的持续部署范围,以及由 Azure 应用服务提供的集成选项。Azure Functions has the full range of continuous deployment and integration options that are provided by Azure App Service. 有关详细信息,请参阅 Azure Functions 的连续部署For more information, see Continuous deployment for Azure Functions.

要加快开发速度,你可能会发现直接通过 .zip 文件部署你的函数应用项目文件更为简单。To speed development, you may find it easier to deploy your function app project files directly from a .zip file. .zip 部署 API 接受 .zip 文件的内容并将内容提取到你的函数应用的 wwwroot 文件夹中。The .zip deployment API takes the contents of a .zip file and extracts the contents into the wwwroot folder of your function app. 此 .zip 文件部署使用相同的 Kudu 服务,支持基于持续集成的部署,包括:This .zip file deployment uses the same Kudu service that powers continuous integration-based deployments, including:

  • 删除之前的部署留下的文件。Deletion of files that were left over from earlier deployments.
  • 部署自定义,包括运行部署脚本。Deployment customization, including running deployment scripts.
  • 部署日志。Deployment logs.
  • 消耗计划函数应用中同步函数触发器。Syncing function triggers in a Consumption plan function app.

有关详细信息,请参阅 .zip 部署参考For more information, see the .zip deployment reference.

部署.zip 文件的要求Deployment .zip file requirements

用于推送部署的 .zip 文件必须包含运行你的函数时所需的所有文件。The .zip file that you use for push deployment must contain all of the files needed to run your function.

重要

使用 .zip 部署时,从函数应用删除现有部署中未包含在 .zip 文件内的任何文件。When you use .zip deployment, any files from an existing deployment that aren't found in the .zip file are deleted from your function app.

特定函数应用中所有函数的代码均位于根项目文件夹中,其中包含主机配置文件和一个或多个子文件夹。The code for all the functions in a specific function app is located in a root project folder that contains a host configuration file and one or more subfolders. 每个子文件夹包含单独函数的代码。Each subfolder contains the code for a separate function. 文件夹结构所示的以下表示形式:The folder structure is shown in the following representation:

FunctionApp
 | - host.json
 | - Myfirstfunction
 | | - function.json
 | | - ...  
 | - mysecondfunction
 | | - function.json
 | | - ...  
 | - SharedCode
 | - bin

在版本 2.x 的 Functions 运行时,函数应用中的所有函数必须共享相同的语言堆栈。In version 2.x of the Functions runtime, all functions in the function app must share the same language stack.

Host.json文件包含特定于运行时的配置,则函数应用根文件夹中。The host.json file contains runtime-specific configurations and is in the root folder of the function app. 一个bin文件夹包含包和函数应用都需要其他库文件。A bin folder contains packages and other library files that the function app requires. 查看函数应用项目的语言特定要求:See the language-specific requirements for a function app project:

函数应用包含 wwwroot 目录中的所有文件和文件夹。A function app includes all of the files and folders in the wwwroot directory. .zip 文件部署包含 wwwroot 目录的内容,但不包含目录本身。A .zip file deployment includes the contents of the wwwroot directory, but not the directory itself. 部署 C# 类库项目时,必须在 .zip 包中的 bin 子文件夹中包括编译的库文件和依赖项。When deploying a C# class library project, you must include the compiled library files and dependencies in a bin subfolder in your .zip package.

下载函数应用文件Download your function app files

在本地计算机上进行开发时,可以轻松在该开发计算机上创建函数应用项目文件夹的 .zip 文件。When you are developing on a local computer, it's easy to create a .zip file of the function app project folder on your development computer.

但是,可能已通过 Azure 门户中的编辑器创建了函数。However, you might have created your functions by using the editor in the Azure portal. 可以通过以下方式之一下载现有的函数应用项目:You can download an existing function app project in one of these ways:

  • 通过 Azure 门户:From the Azure portal:

    1. 登录 Azure 门户,并转到函数应用。Sign in to the Azure portal, and then go to your function app.

    2. 在“概述”选项卡中,选择“下载应用内容” 。On the Overview tab, select Download app content. 选择下载选项,然后选择“下载” 。Select your download options, and then select Download.

      下载函数应用项目

      所下载的 .zip 文件格式正确,可使用 .zip 推送部署将其重新发布到函数应用。The downloaded .zip file is in the correct format to be republished to your function app by using .zip push deployment. 门户下载还可以添加在 Visual Studio 中直接打开函数应用所需的文件。The portal download can also add the files needed to open your function app directly in Visual Studio.

  • 使用 REST API:Using REST APIs:

    使用以下部署 GET API 从 <function_app> 项目中下载文件:Use the following deployment GET API to download the files from your <function_app> project:

      https://<function_app>.scm.azurewebsites.net/api/zip/site/wwwroot/
    

    包含 /site/wwwroot/ 可确保 zip 文件仅包含函数应用项目文件,而不是整个站点。Including /site/wwwroot/ makes sure your zip file includes only the function app project files and not the entire site. 如果尚未登录 Azure,系统会要求你登录。If you are not already signed in to Azure, you will be asked to do so.

还可从 GitHub 存储库下载 .zip 文件。You can also download a .zip file from a GitHub repository. 在以 .zip 文件形式下载 GitHub 存储库时,GitHub 会为分支添加一个额外的文件夹级别。When you download a GitHub repository as a .zip file, GitHub adds an extra folder level for the branch. 此额外的文件夹级别意味着无法直接部署从 GitHub 下载的 .zip 文件。This extra folder level means that you can't deploy the .zip file directly as you downloaded it from GitHub. 如果当前使用 GitHub 存储库维护函数应用,应使用持续集成来部署应用。If you're using a GitHub repository to maintain your function app, you should use continuous integration to deploy your app.

使用 Azure CLI 进行部署Deploy by using Azure CLI

可使用 Azure CLI 来触发推送部署。You can use Azure CLI to trigger a push deployment. 使用 az functionapp deployment source config-zip 命令将 .zip 文件推送部署到函数应用。Push deploy a .zip file to your function app by using the az functionapp deployment source config-zip command. 要使用此命令,必须使用 Azure CLI 版本 2.0.21 或更高版本。To use this command, you must use Azure CLI version 2.0.21 or later. 要查看当前使用的 Azure CLI 版本,请使用 az --version 命令。To see what Azure CLI version you are using, use the az --version command.

以下命令将 <zip_file_path> 占位符替换为 .zip 文件的位置路径。In the following command, replace the <zip_file_path> placeholder with the path to the location of your .zip file. 此外,<app_name> 替换为函数应用的唯一名称。Also, replace <app_name> with the unique name of your function app.

az functionapp deployment source config-zip  -g myResourceGroup -n \
<app_name> --src <zip_file_path>

此命令将项目文件从下载的 .zip 文件部署到 Azure 中的函数应用。This command deploys project files from the downloaded .zip file to your function app in Azure. 然后重启应用。It then restarts the app. 要查看此函数应用的部署列表,必须使用 REST API。To view the list of deployments for this function app, you must use the REST APIs.

在本地计算机上使用 Azure CLI 时,<zip_file_path> 是计算机中 .zip 文件的路径。When you're using Azure CLI on your local computer, <zip_file_path> is the path to the .zip file on your computer. 也可以在 Azure Cloud Shell 中运行 Azure CLI。You can also run Azure CLI in Azure Cloud Shell. 使用 Cloud Shell 时,首先必须将部署 .zip 文件上传到与 Cloud Shell 关联的 Azure 文件帐户。When you use Cloud Shell, you must first upload your deployment .zip file to the Azure Files account that's associated with your Cloud Shell. 在这种情况下,<zip_file_path> 是 Cloud Shell 帐户使用的存储位置。In that case, <zip_file_path> is the storage location that your Cloud Shell account uses. 有关详细信息,请参阅在 Azure Cloud Shell 中持久保存文件For more information, see Persist files in Azure Cloud Shell.

使用 REST API 部署 ZIP 文件Deploy ZIP file with REST APIs

可以使用部署服务 REST API 将 .zip 文件部署到 Azure 中的应用。You can use the deployment service REST APIs to deploy the .zip file to your app in Azure. 若要部署,请将 POST 请求发送到 https://<app_name>.scm.azurewebsites.net/api/zipdeploy。To deploy, send a POST request to https://<app_name>.scm.azurewebsites.net/api/zipdeploy. POST 请求必须在消息正文中包含此 .zip 文件。The POST request must contain the .zip file in the message body. 应用的部署凭据是通过使用 HTTP BASIC 身份验证在请求中提供的。The deployment credentials for your app are provided in the request by using HTTP BASIC authentication. 有关详细信息,请参阅 .zip 推送部署参考For more information, see the .zip push deployment reference.

对于 HTTP 基本身份验证,需使用应用服务部署凭据。For the HTTP BASIC authentication, you need your App Service deployment credentials. 若要了解如何设置部署凭据,请参阅设置和重置用户级别凭据To see how to set your deployment credentials, see Set and reset user-level credentials.

使用 cURLWith cURL

以下示例使用 cURL 工具部署 .zip 文件。The following example uses the cURL tool to deploy a .zip file. 替换占位符 <username><password><zip_file_path><app_name>Replace the placeholders <username>, <password>, <zip_file_path>, and <app_name>. 出现 cURL 提示时,键入密码。When prompted by cURL, type in the password.

curl -X POST -u <deployment_user> --data-binary @"<zip_file_path>" https://<app_name>.scm.azurewebsites.net/api/zipdeploy

此请求从已上传的 .zip 文件触发推送部署。This request triggers push deployment from the uploaded .zip file. 可以使用 https://<app_name>.scm.azurewebsites.net/api/deployments 终结点查看当前和之前的部署,如以下 cURL 示例所示。You can review the current and past deployments by using the https://<app_name>.scm.azurewebsites.net/api/deployments endpoint, as shown in the following cURL example. 同样,使用应用的名称替换 <app_name>;使用部署凭据的用户名替换 <deployment_user>Again, replace <app_name> with the name of your app and <deployment_user> with the username of your deployment credentials.

curl -u <deployment_user> https://<app_name>.scm.azurewebsites.net/api/deployments

使用 PowerShellWith PowerShell

下面的示例使用AzWebapp上传 .zip 文件。The following example uses Publish-AzWebapp upload the .zip file. 替换占位符 <group-name><app-name><zip-file-path>Replace the placeholders <group-name>, <app-name>, and <zip-file-path>.

Publish-AzWebapp -ResourceGroupName <group-name> -Name <app-name> -ArchivePath <zip-file-path>

此请求从已上传的 .zip 文件触发推送部署。This request triggers push deployment from the uploaded .zip file.

若要查看当前和之前的部署,请运行以下命令。To review the current and past deployments, run the following commands. 再次替换<deployment-user><deployment-password><app-name>占位符。Again, replace the <deployment-user>, <deployment-password>, and <app-name> placeholders.

$username = "<deployment-user>"
$password = "<deployment-password>"
$apiUrl = "https://<app-name>.scm.azurewebsites.net/api/deployments"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password)))
$userAgent = "powershell/1.0"
Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method GET

从部署包中运行函数Run functions from the deployment package

还可以选择直接从部署包文件中运行你的函数。You can also choose to run your functions directly from the deployment package file. 此方法跳过了将文件从包中复制到函数应用的 wwwroot 目录这一部署步骤。This method skips the deployment step of copying files from the package to the wwwroot directory of your function app. 包文件将由 Functions 运行时进行装载,并且 wwwroot 目录的内容将变为只读的。Instead, the package file is mounted by the Functions runtime, and the contents of the wwwroot directory become read-only.

Zip 部署集成了此功能,可以通过将函数应用设置 WEBSITE_RUN_FROM_PACKAGE 设置为 1 值来启用此功能。Zip deployment integrates with this feature, which you can enable by setting the function app setting WEBSITE_RUN_FROM_PACKAGE to a value of 1. 有关详细信息,请参阅从部署包文件运行函数For more information, see Run your functions from a deployment package file.

部署自定义Deployment customization

部署过程假设推送的 .zip 文件包含随时可运行的应用。The deployment process assumes that the .zip file that you push contains a ready-to-run app. 默认情况下,不会运行自定义。By default, no customizations are run. 若要启用通过持续集成获取的同一生成进程,请将以下内容添加到应用程序设置:To enable the same build processes that you get with continuous integration, add the following to your application settings:

SCM_DO_BUILD_DURING_DEPLOYMENT=true 

使用 .zip 推送部署时,此设置默认为“false” 。When you use .zip push deployment, this setting is false by default. 持续集成部署的设置默认为“true” 。The default is true for continuous integration deployments. 设置为“true”时,在部署期间将使用与部署相关的设置 。When set to true, your deployment-related settings are used during deployment. 可以将这些设置配置为应用设置或在位于 .zip 文件根目录中的 .deployment 配置文件中进行配置。You can configure these settings either as app settings or in a .deployment configuration file that's located in the root of your .zip file. 有关详细信息,请参阅部署参考中的 Repository and deployment-related settings(存储库和与部署相关的设置)。For more information, see Repository and deployment-related settings in the deployment reference.

后续步骤Next steps