您现在访问的是微软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 > / u。<扩展 > 下_应用_数据/作业/continuous_连续 web 作业,或_应用_数据/作业/triggered_触发Web 作业。<job_name>/run.<extension> under App_Data/jobs/continuous for continuous WebJobs, or App_Data/jobs/triggered for triggered WebJobs. 有关详细信息,请参阅 Kudu WebJobs 文档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 门户左侧导航栏中选择 "应用服务", 然后选择要部署的 web 应用。Select App Services in the Azure portal left navigation, and then select the web app you want to deploy.

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

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

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

  4. 如有必要, 请登录到服务, 并按照授权提示进行操作。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:使用应用服务生成服务Option 1: Use the App Service build 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 门户左侧导航栏中选择 "应用服务", 然后选择要部署的 web 应用。Select App Services in the Azure portal left navigation, and then select the web app 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.

    • 对于 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.

    所选存储库和分支中的新提交现在会连续部署到应用服务应用。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: Use 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 repositories. 有关通过 Azure Pipelines 部署的详细信息, 请参阅将 web 应用部署到 Azure 应用服务For more information about deploying through Azure Pipelines, see Deploy a web app to Azure App Services.

对于在 Azure DevOps 组织中创建持续交付 Azure Pipelines 的 Azure App Service:For Azure App Service to create continuous delivery Azure Pipelines in your Azure DevOps organization:

  • 你的 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.

配置 Azure Pipelines (预览版):To configure Azure Pipelines (Preview):

  1. Azure 门户左侧导航栏中选择 "应用服务", 然后选择要部署的 web 应用。Select App Services in the Azure portal left navigation, and then select the web app you want to deploy.

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

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

  4. 在 "配置" 页上的 "代码" 部分中:On the Configure page, in the Code section:

    • 对于 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.

    • 对于 "Azure Repos", 请选择要连续部署的Azure DevOps 组织项目存储库分支, 或配置新的 azure DevOps 组织。For Azure Repos, select the Azure DevOps Organization, Project, Repository, and Branch you want to deploy continuously, or configure a new Azure DevOps organization.

      备注

      如果未列出现有的 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.

  5. 选择继续Select Continue.

  6. 对于 "Azure Repos", 请在 "生成" 部分中指定 Azure Pipelines 应用于运行生成任务的语言框架, 然后选择 "继续"。For Azure Repos, in the Build section, specify the language framework that Azure Pipelines should use to run build tasks, and then select Continue.

  7. 在 "测试" 页上, 选择是否要启用负载测试, 然后选择 "继续"。On the Test page, choose whether to enable load tests, and then select Continue.

  8. 根据你的应用服务计划定价层, 你可能会看到 "部署到过渡" 页。Depending on your App Service plan pricing tier, you may see a Deploy to staging page. 选择是否启用部署槽位, 然后选择 "继续"。Choose whether to enable deployment slots, and then select Continue.

    备注

    Azure Pipelines 不允许持续交付到生产槽。Azure Pipelines doesn't allow continuous delivery to the production slot. 此限制可防止意外部署到生产环境。This restriction prevents accidental deployments to production. 设置到过渡槽的连续交付, 验证是否存在更改, 然后在准备就绪时交换槽。Set up continuous delivery to a staging slot, verify the changes there, and then swap the slots when you are ready.

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

    所选存储库和分支中的新提交现在会连续部署到应用服务应用。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.

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

禁用持续部署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 the same ones that are run in production. 因此,部署可能会因锁定的文件而失败。Therefore, the deployment can fail because of locked files. 在生产环境中的应用还行为可能无法预测在部署期间,因为并非所有文件在同一时间都更新。The app in production may also behave unpredictably during deployment, because not all the files updated at the same time. 可通过多种不同方式避免这些问题:There are a few different ways to avoid these issues:

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

对于 Windows 应用, 你可以从门户不直接支持的云 Git 或 Mecurial 存储库 (如GitLab) 手动配置连续部署。For Windows apps, you can manually configure continuous deployment from a cloud Git or Mecurial 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