使用 ZIP 或 WAR 文件将应用部署到 Azure 应用服务Deploy your app to Azure App Service with a ZIP or WAR file

本文演示如何使用 ZIP 或 WAR 文件将 Web 应用部署到 Azure 应用服务This article shows you how to use a ZIP file or WAR file to deploy your web app to Azure App Service.

此 .zip 文件部署使用相同的 Kudu 服务,支持基于持续集成的部署。This ZIP file deployment uses the same Kudu service that powers continuous integration-based deployments. 对于 .zip 文件部署,Kudu 支持以下功能:Kudu supports the following functionality for ZIP file deployment:

  • 删除先前的部署留下的文件。Deletion of files left over from a previous deployment.
  • 启用默认生成过程的选项,包括包还原。Option to turn on the default build process, which includes package restore.
  • 部署自定义,包括运行部署脚本。Deployment customization, including running deployment scripts.
  • 部署日志。Deployment logs.
  • 文件大小上限是 2048 MB。A file size limit of 2048 MB.

有关详细信息,请参阅 Kudu 文档For more information, see Kudu documentation.

WAR 文件部署将 WAR 文件部署到应用服务,以运行 Java Web 应用。The WAR file deployment deploys your WAR file to App Service to run your Java web app. 请参阅部署 WAR 文件See Deploy WAR file.

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

先决条件Prerequisites

完成本文中的步骤:To complete the steps in this article:

创建一个项目 zip 文件Create a project ZIP file

Note

如果以 zip 文件的形式下载文件,请首先提取该文件。If you downloaded the files in a ZIP file, extract the files first. 例如,如果从 GitHub 下载了 zip 文件,则无法按原样部署该文件。For example, if you downloaded a ZIP file from GitHub, you cannot deploy that file as-is. GitHub 可添加额外的嵌套目录,但这不适用于应用服务。GitHub adds additional nested directories, which do not work with App Service.

在本地终端窗口中,导航到应用项目的根目录。In a local terminal window, navigate to the root directory of your app project.

此目录应包含 Web 应用的入口文件,例如 index.htmlindex.phpapp.jsThis directory should contain the entry file to your web app, such as index.html, index.php, and app.js. 它还可能包含包管理文件,如 project.jsoncomposer.jsonpackage.jsonbower.jsonrequirements.txtIt can also contain package management files like project.json, composer.json, package.json, bower.json, and requirements.txt.

创建一个包含项目中的所有内容的 zip 存档。Create a ZIP archive of everything in your project. 以下命令使用您终端中的默认工具执行操作:The following command uses the default tool in your terminal:

# Bash
zip -r <file-name>.zip .

# PowerShell
Compress-Archive -Path * -DestinationPath <file-name>.zip

部署 ZIP 文件Deploy ZIP file

在浏览器中,导航到 https://<app_name>.scm.chinacloudsites.cn/ZipDeployUIIn the browser, navigate to https://<app_name>.scm.chinacloudsites.cn/ZipDeployUI.

上传在创建一个项目 ZIP 文件中创建的 ZIP 文件,只需将其拖至网页的文件资源管理器区域即可。Upload the ZIP file you created in Create a project ZIP file by dragging it to the file explorer area on the web page.

部署正在进行的时候,右上角的图标会以百分比形式显示进度。When deployment is in progress, an icon in the top right corner shows you the progress in percentage. 该页还在资源管理器区域下方显示操作的详细消息。The page also shows verbose messages for the operation below the explorer area. 完成后,最后一个部署消息会显示:Deployment successfulWhen it is finished, the last deployment message should say Deployment successful.

使用 Azure CLI 部署 ZIP 文件Deploy ZIP file with Azure CLI

请确保 Azure CLI 版本是 2.0.21 或更高版本。Make sure your Azure CLI version is 2.0.21 or later. 要查看已有版本,请在终端窗口中运行 az --version 命令。To see which version you have, run az --version command in your terminal window.

使用 az webapp deployment source config-zip 命令将上传的 zip 文件部署到 Web 应用。Deploy the uploaded ZIP file to your web app by using the az webapp deployment source config-zip command.

下面的示例部署上传的 zip 文件。The following example deploys the ZIP file you uploaded. 使用 Azure CLI 的本地安装时,请为 --src 指定本地 zip 文件的路径。When using a local installation of Azure CLI, specify the path to your local ZIP file for --src.

az webapp deployment source config-zip --resource-group myResourceGroup --name <app_name> --src clouddrive/<filename>.zip

此命令将 zip 文件中的文件和目录部署到默认的应用服务应用程序文件夹 (\home\site\wwwroot) 并重启应用。This command deploys the files and directories from the ZIP file to your default App Service application folder (\home\site\wwwroot) and restarts the app.

默认情况下,部署引擎假定 ZIP 文件已准备好按原样运行,并且不运行任何生成自动化。By default, the deployment engine assumes that a ZIP file is ready to run as-is and doesn't run any build automation. 若要启用与 Git 部署中相同的生成自动化,请通过在 CLI 中运行以下命令来设置 SCM_DO_BUILD_DURING_DEPLOYMENT 应用设置:To enable the same build automation as in a Git deployment, set the SCM_DO_BUILD_DURING_DEPLOYMENT app setting by running the following command in the CLI:

az webapp config appsettings set --resource-group <resource-group-name> --name <app-name> --settings SCM_DO_BUILD_DURING_DEPLOYMENT=true

有关详细信息,请参阅 Kudu 文档For more information, see Kudu documentation.

使用 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://<应用名称>.scm.chinacloudsites.cn/api/zipdeploy。To deploy, send a POST request to https://<app_name>.scm.chinacloudsites.cn/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.chinacloudsites.cn/api/zipdeploy

此请求从已上传的 .zip 文件触发推送部署。This request triggers push deployment from the uploaded .zip file. 可以使用 https://<app_name>.scm.chinacloudsites.cn/api/deployments 终结点查看当前和之前的部署,如以下 cURL 示例所示。You can review the current and past deployments by using the https://<app_name>.scm.chinacloudsites.cn/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.chinacloudsites.cn/api/deployments

使用 PowerShellWith PowerShell

以下示例使用 Invoke-RestMethod 发送包含 .zip 文件的请求。The following example uses Invoke-RestMethod to send a request that contains the .zip file. 替换占位符 <deployment_user><deployment_password><zip_file_path><app_name>Replace the placeholders <deployment_user>, <deployment_password>, <zip_file_path>, and <app_name>.

#PowerShell
$username = "<deployment_user>"
$password = "<deployment_password>"
$filePath = "<zip_file_path>"
$apiUrl = "https://<app_name>.scm.chinacloudsites.cn/api/zipdeploy"
$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 POST -InFile $filePath -ContentType "multipart/form-data"

此请求从已上传的 .zip 文件触发推送部署。This request triggers push deployment from the uploaded .zip file. 若要查看当前和之前的部署,请运行以下命令。To review the current and past deployments, run the following commands. 同样,请替换 <app_name> 占位符。Again, replace the <app_name> placeholder.

$apiUrl = "https://<app_name>.scm.chinacloudsites.cn/api/deployments"
Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method GET

部署 WAR 文件Deploy WAR file

若要将 WAR 文件部署到应用服务,请将 POST 请求发送到 https://<app_name>.scm.chinacloudsites.cn/api/wardeploy。To deploy a WAR file to App Service, send a POST request to https://<app_name>.scm.chinacloudsites.cn/api/wardeploy. POST 请求必须在消息正文中包含此 .war 文件。The POST request must contain the .war file in the message body. 应用的部署凭据是通过使用 HTTP BASIC 身份验证在请求中提供的。The deployment credentials for your app are provided in the request by using HTTP BASIC authentication.

对于 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 工具部署 .war 文件。The following example uses the cURL tool to deploy a .war file. 替换占位符 <username><war_file_path><app_name>Replace the placeholders <username>, <war_file_path>, and <app_name>. 出现 cURL 提示时,键入密码。When prompted by cURL, type in the password.

curl -X POST -u <username> --data-binary @"<war_file_path>" https://<app_name>.scm.chinacloudsites.cn/api/wardeploy

使用 PowerShellWith PowerShell

以下示例使用 Invoke-RestMethod 发送包含 .war 文件的请求。The following example uses Invoke-RestMethod to send a request that contains the .war file. 替换占位符 <deployment_user><deployment_password><zip_file_path><app_name>Replace the placeholders <deployment_user>, <deployment_password>, <zip_file_path>, and <app_name>.

$username = "<deployment_user>"
$password = "<deployment_password>"
$filePath = "<war_file_path>"
$apiUrl = "https://<app_name>.scm.chinacloudsites.cn/api/wardeploy"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password)))
Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST -InFile $filePath -ContentType "application/octet-stream"

部署期间我的应用会发生什么情况?What happens to my app during deployment?

所有官方支持的部署方法均具有一个共同点:它们会更改应用的 /home/site/wwwroot 文件夹中的文件。All the officially supported deployment methods have one thing in common: they make changes to the files in the /home/site/wwwroot folder of your app. 这些文件与生产中运行的文件相同。These are the same files that are run in production. 因此,部署可能由于存在锁定文件而失败,或者由于并非所有文件同时更新,生产中的应用在部署期间可能出现不可预测的行为。Therefore, the deployment can fail due to locked files, or the app in production may have unpredictable behavior during deployment because not all the files are updated simultaneously. 可通过多种不同方式避免这些问题:There are a few different ways to avoid these issues:

后续步骤Next steps

有关更高级的部署方案,请参阅使用 Git 部署到 AzureFor more advanced deployment scenarios, try deploying to Azure with Git. 通过基于 Git 的 Azure 部署可实现版本控制、包还原、MSBuild 等功能。Git-based deployment to Azure enables version control, package restore, MSBuild, and more.

更多资源More resources