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

持续部署到 Azure 应用服务Continuous deployment to Azure App Service

Azure App Service可以通过拉取最新的更新,从 GitHub、BitBucket 和Azure Repos存储库进行连续部署。Azure App Service enables continuous deployment from GitHub, BitBucket, and Azure Repos repositories by pulling in the latest updates. 本文介绍如何使用 Azure 门户通过 Kudu 生成服务或Azure Pipelines持续部署应用。This article shows you how to use the Azure portal to continuously deploy your app through the Kudu build service or Azure Pipelines.

有关源代码管理服务的详细信息,请参阅[创建存储库(GitHub)]、[创建存储库(BitBucket)]或创建新的 Git 存储库(Azure Repos)For more information on the source control services, see Create a repo (GitHub), Create a repo (BitBucket), or Create a new Git repo (Azure Repos).

准备存储库Prepare your repository

若要从 Azure 应用服务 Kudu 生成服务器获取自动生成,请确保项目中存储库根路径具有正确的文件。To get automatic builds from Azure App Service Kudu build server, make sure that your repository root has the correct files in your project.

运行时Runtime 根目录文件Root directory files
ASP.NET(仅限 Windows)ASP.NET (Windows only) *.sln*.csprojdefault.aspx*.sln, *.csproj, or default.aspx
ASP.NET CoreASP.NET Core *.sln*.csproj*.sln or *.csproj
PHPPHP index.php index.php
Ruby(仅限 Linux)Ruby (Linux only) Gemfile Gemfile
Node.jsNode.js server.js、app.js 或具有启动脚本的 package.json server.js, app.js, or package.json with a start script
PythonPython *.pyrequirements.txtruntime.txt*.py, requirements.txt, or runtime.txt
HTMLHTML default.htm、default.html、default.asp、index.htm、index.html 或 iisstart.htm default.htm, default.html, default.asp, index.htm, index.html, or iisstart.htm
Web 作业WebJobs <job_name>/run.<extension><job_name>/run.<extension> 适用于连续 Web 作业的 "应用 _ 数据/作业/连续" 或 "_应用 _ 数据/作业/已触发_的 web 作业"。under App_Data/jobs/continuous for continuous WebJobs, or App_Data/jobs/triggered for triggered WebJobs. 有关详细信息,请参阅Kudu Web 作业文档For more information, see Kudu WebJobs documentation.
函数Functions 请参阅 Azure Functions 的连续部署See Continuous deployment for Azure Functions.

要自定义部署,可以在存储库根路径中添加 .deployment 文件**。To customize your deployment, include a .deployment file in the repository root. 有关详细信息,请参阅自定义部署自定义部署脚本For more information, see Customize deployments and Custom deployment script.

备注

如果在 Visual Studio 中进行开发,让 Visual Studio 创建存储库If you develop in Visual Studio, let Visual Studio create a repository for you. 该项目可立即通过 Git 进行部署。The project is immediately ready to be deployed by using Git.

授权 Azure App ServiceAuthorize Azure App Service

若要使用 Azure Repos,请确保 Azure DevOps 组织已链接到 Azure 订阅。To use Azure Repos, make sure your Azure DevOps organization is linked to your Azure subscription. 有关详细信息,请参阅设置 Azure DevOps Services 帐户,以便它可以部署到 web 应用For more information, see Set up an Azure DevOps Services account so it can deploy to a web app.

对于 Bitbucket 或 GitHub,授权 Azure App Service 连接到存储库。For Bitbucket or GitHub, authorize Azure App Service to connect to your repository. 只需使用源代码管理服务进行一次授权。You only need to authorize with a source control service once.

  1. Azure 门户中,搜索 "应用服务" 并选择。In the Azure portal, search for App Services and select.

    搜索应用服务。

  2. 选择要部署的应用服务。Select the App Service you want to deploy.

    选择应用。

  3. 在应用页的左侧菜单中,选择 "部署中心"。On the app page, select Deployment Center in the left menu.

  4. 在 "部署中心" 页上,选择 " GitHub " 或 " Bitbucket",然后选择 "授权"。On the Deployment Center page, select GitHub or Bitbucket, and then select Authorize.

    选择 "源代码管理" 服务,然后选择 "授权"。

  5. 如有必要,请登录到服务,并按照授权提示进行操作。Sign in to the service if necessary, and follow the authorization prompts.

启用持续部署Enable continuous deployment

授权源代码管理服务后,通过内置的Kudu 应用服务生成服务器或Azure Pipelines,将应用配置为连续部署。After you authorize a source control service, configure your app for continuous deployment through the built-in Kudu App Service build server, or through Azure Pipelines.

选项1: Kudu 应用服务Option 1: Kudu App Service

你可以使用内置的 Kudu 应用服务生成服务器从 GitHub、Bitbucket 或 Azure Repos 持续进行部署。You can use the built-in Kudu App Service build server to continuously deploy from GitHub, Bitbucket, or Azure Repos.

  1. Azure 门户中,搜索 "应用服务",然后选择要部署的应用服务。In the Azure portal, search for App Services, and then select the App Service you want to deploy.

  2. 在应用页的左侧菜单中,选择 "部署中心"。On the app page, select Deployment Center in the left menu.

  3. 在 "部署中心" 页上选择授权的源代码管理提供程序,并选择 "继续"。Select your authorized source control provider on the Deployment Center page, and select Continue. 对于 GitHub 或 Bitbucket,还可以选择 "更改帐户" 以更改授权帐户。For GitHub or Bitbucket, you can also select Change account to change the authorized account.

    备注

    若要使用 Azure Repos,请确保 Azure DevOps Services 组织已链接到 Azure 订阅。To use Azure Repos, make sure your Azure DevOps Services organization is linked to your Azure subscription. 有关详细信息,请参阅设置 Azure DevOps Services 帐户,以便它可以部署到 web 应用For more information, see Set up an Azure DevOps Services account so it can deploy to a web app.

  4. 对于 GitHub 或 Azure Repos,请在 "生成提供程序" 页上选择 "应用服务生成服务",然后选择 "继续"。For GitHub or Azure Repos, on the Build provider page, select App Service build service, and then select Continue. Bitbucket 始终使用应用服务生成服务。Bitbucket always uses the App Service build service.

    选择 "应用服务生成服务",并选择 "继续"。

  5. 在 "配置" 页上:On the Configure page:

    • 对于 GitHub,请下拉菜单,然后选择要连续部署的组织存储库分支For GitHub, drop down and select the Organization, Repository, and Branch you want to deploy continuously.

      备注

      如果看不到任何存储库,可能需要在 GitHub 中授权 Azure App Service。If you don't see any repositories, you may need to authorize Azure App Service in GitHub. 浏览到 GitHub 存储库,转到 "设置" " > 应用程序" "已 > 授权 OAuth 应用"。Browse to your GitHub repository and go to Settings > Applications > Authorized OAuth Apps. 选择 " Azure App Service",然后选择 "授予"。Select Azure App Service, and then select Grant. 对于组织存储库,你必须是组织的所有者才能授予权限。For organization repositories, you must be an owner of the organization to grant the permissions.

    • 对于 Bitbucket,选择要连续部署的 Bitbucket团队存储库分支For Bitbucket, select the Bitbucket Team, Repository, and Branch you want to deploy continuously.

    • 对于 "Azure Repos",请选择要连续部署的Azure DevOps 组织项目存储库分支For Azure Repos, select the Azure DevOps Organization, Project, Repository, and Branch you want to deploy continuously.

      备注

      如果未列出 Azure DevOps 组织,请确保已将它链接到 Azure 订阅。If your Azure DevOps organization isn't listed, make sure it's linked to your Azure subscription. 有关详细信息,请参阅设置 Azure DevOps Services 帐户,以便它可以部署到 web 应用For more information, see Set up an Azure DevOps Services account so it can deploy to a web app..

  6. 选择继续Select Continue.

    填写 "存储库信息",然后选择 "继续"。

  7. 配置生成提供程序之后,查看 "摘要" 页上的设置,然后选择 "完成"。After you configure the build provider, review the settings on the Summary page, and then select Finish.

  8. 选定存储库和分支中的新提交现在将持续部署到应用服务应用中。New commits in the selected repository and branch now deploy continuously into your App Service app. 可以在“部署中心”页上跟踪提交和部署。You can track the commits and deployments on the Deployment Center page.

    在部署中心跟踪提交和部署

选项2: Azure PipelinesOption 2: Azure Pipelines

如果帐户具有所需的权限,则可以设置 Azure Pipelines 从 GitHub 或 Azure Repos 持续部署。If your account has the necessary permissions, you can set up Azure Pipelines to continuously deploy from GitHub or Azure Repos. 有关通过 Azure Pipelines 部署的详细信息,请参阅将 web 应用部署到 Azure 应用服务For more information about deploying through Azure Pipelines, see Deploy a web app to Azure App Services.

先决条件Prerequisites

若要 Azure App Service 使用 Azure Pipelines 创建持续交付,Azure DevOps 组织应具有以下权限:For Azure App Service to create continuous delivery using Azure Pipelines, your Azure DevOps organization should have the following permissions:

  • 你的 Azure 帐户必须具有写入 Azure Active Directory 和创建服务的权限。Your Azure account must have permissions to write to Azure Active Directory and create a service.

  • 你的 Azure 帐户必须拥有 Azure 订阅中的 "所有者" 角色。Your Azure account must have the Owner role in your Azure subscription.

  • 你必须是要使用的 Azure DevOps 项目中的管理员。You must be an administrator in the Azure DevOps project you want to use.

GitHub + Azure PipelinesGitHub + Azure Pipelines

  1. Azure 门户中,搜索 "应用服务",然后选择要部署的应用服务。In the Azure portal, search for App Services, and then select the App Service you want to deploy.

  2. 在应用页的左侧菜单中,选择 "部署中心"。On the app page, select Deployment Center in the left menu.

  3. 选择 " GitHub " 作为 "部署中心" 页上的源代码管理提供程序,然后选择 "继续"。Select GitHub as the source control provider on the Deployment Center page and select Continue. 对于GitHub,你可以选择 "更改帐户" 以更改授权帐户。For GitHub, you can select Change Account to change the authorized account.

    源代码管理 (source control)

  4. 在 "生成提供程序" 页上,选择 " Azure Pipelines (预览)",然后选择 "继续"。On the Build Provider page, select Azure Pipelines (Preview), and then select Continue.

    生成提供程序

  5. 在 "配置" 页上的 "代码" 部分中,选择要连续部署的组织存储库分支,然后选择 "继续"。On the Configure page, in the Code section, select the Organization, Repository, and Branch you want to deploy continuously and select Continue.

    备注

    如果看不到任何存储库,可能需要在 GitHub 中授权 Azure App Service。If you don't see any repositories, you may need to authorize Azure App Service in GitHub. 浏览到 GitHub 存储库,转到 "设置" " > 应用程序" "已 > 授权 OAuth 应用"。Browse to your GitHub repository and go to Settings > Applications > Authorized OAuth Apps. 选择 " Azure App Service",然后选择 "授予"。Select Azure App Service, and then select Grant. 对于组织存储库,你必须是组织的所有者才能授予权限。For organization repositories, you must be an owner of the organization to grant the permissions.

    在 "生成" 部分中,指定 Azure Pipelines 应用于运行生成任务的 Azure DevOps 组织、项目、语言框架,然后选择 "继续"。In the Build section, specify the Azure DevOps Organization, Project, language framework that Azure Pipelines should use to run build tasks, and then select Continue.

    生成提供程序

  6. 配置生成提供程序之后,查看 "摘要" 页上的设置,然后选择 "完成"。After you configure the build provider, review the settings on the Summary page, and then select Finish.

    生成提供程序

  7. 所选存储库和分支中的新提交现在会连续部署到应用服务中。New commits in the selected repository and branch now deploy continuously into your App Service. 可以在“部署中心”页上跟踪提交和部署。You can track the commits and deployments on the Deployment Center page.

    在部署中心跟踪提交和部署

Azure Repos + Azure PipelinesAzure Repos + Azure Pipelines

  1. Azure 门户中,搜索 "应用服务",然后选择要部署的应用服务。In the Azure portal, search for App Services, and then select the App Service you want to deploy.

  2. 在应用页的左侧菜单中,选择 "部署中心"。On the app page, select Deployment Center in the left menu.

  3. 选择 " Azure Repos作为"部署中心"页上的源代码管理提供程序,然后选择"继续"。Select Azure Repos as the source control provider on the Deployment Center page and select Continue.

    源代码管理 (source control)

  4. 在 "生成提供程序" 页上,选择 " Azure Pipelines (预览)",然后选择 "继续"。On the Build Provider page, select Azure Pipelines (Preview), and then select Continue.

    源代码管理 (source control)

  5. 在 "配置" 页上的 "代码" 部分中,选择要连续部署的组织存储库分支,然后选择 "继续"。On the Configure page, in the Code section, select the Organization, Repository, and Branch you want to deploy continuously and select Continue.

    备注

    如果未列出现有的 Azure DevOps 组织,可能需要将其链接到 Azure 订阅。If your existing Azure DevOps organization isn't listed, you may need to link it to your Azure subscription. 有关详细信息,请参阅定义 CD 发布管道For more information, see Define your CD release pipeline.

    在 "生成" 部分中,指定 Azure Pipelines 应用于运行生成任务的 Azure DevOps 组织、项目、语言框架,然后选择 "继续"。In the Build section, specify the Azure DevOps Organization, Project, language framework that Azure Pipelines should use to run build tasks, and then select Continue.

    生成提供程序

  6. 配置生成提供程序之后,查看 "摘要" 页上的设置,然后选择 "完成"。After you configure the build provider, review the settings on the Summary page, and then select Finish.

    生成提供程序

  7. 所选存储库和分支中的新提交现在会连续部署到应用服务中。New commits in the selected repository and branch now deploy continuously into your App Service. 可以在“部署中心”页上跟踪提交和部署。You can track the commits and deployments on the Deployment Center page.

禁用持续部署Disable continuous deployment

若要禁用连续部署,请在应用的部署中心页面顶部选择 "断开连接"。To disable continuous deployment, select Disconnect at the top of your app's Deployment Center page.

禁用持续部署

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

所有官方支持的部署方法都会更改应用的 /home/site/wwwroot 文件夹中的文件。All the officially supported deployment methods make changes to the files in the /home/site/wwwroot folder of your app. 这些文件用于运行你的应用。These files are used to run your app. 因此,部署可能会因为文件锁定而失败。Therefore, the deployment can fail because of locked files. 应用在部署期间的行为也可能无法预测,因为并非所有文件都同时更新。The app may also behave unpredictably during deployment, because not all the files updated at the same time. 这对于面向客户的应用来说是不合需要的。This is undesirable for a customer-facing app. 可通过多种不同方式避免这些问题:There are a few different ways to avoid these issues:

使用不支持的存储库Use unsupported repos

对于 Windows 应用,你可以从门户不直接支持的云 Git 或 Mercurial 存储库手动配置连续部署,如GitLabFor Windows apps, you can manually configure continuous deployment from a cloud Git or Mercurial repository that the portal doesn't directly support, such as GitLab. 可以通过在 "部署中心" 页中选择 "外部" 框来执行此操作。You do it by choosing the External box in the Deployment Center page. 有关详细信息,请参阅使用手动步骤设置连续部署For more information, see Set up continuous deployment using manual steps.

其他资源Additional resources