Define your multi-stage continuous deployment (CD) pipeline(定义多阶段持续部署 (CD) 管道)Define your multi-stage continuous deployment (CD) pipeline

Azure Pipelines | Azure DevOps Server 2020 | Azure DevOps Server 2019 | TFS 2018 - TFS 2015Azure Pipelines | Azure DevOps Server 2020 | Azure DevOps Server 2019 | TFS 2018 - TFS 2015

备注

在 Microsoft Team Foundation Server (TFS) 2018 和更低版本中,生成和发布管道被称为“定义”,运行被称为“生成”,服务连接被称为“服务终结点”,阶段被称为“环境”,而作业被称为“阶段” 。In Microsoft Team Foundation Server (TFS) 2018 and previous versions, build and release pipelines are called definitions, runs are called builds, service connections are called service endpoints, stages are called environments, and jobs are called phases.

Azure Pipelines 为发布到多个阶段(如开发、过渡、QA 和生产)提供高度可配置和可管理的管道。Azure Pipelines provide a highly configurable and manageable pipeline for releases to multiple stages such as development, staging, QA, and production. 它还提供在每个特定阶段实施入口和批准的机会。it also offers the opportunity to implement gates and approvals at each specific stage.

本教程的内容:In this tutorial, you will learn about:

  • 连续部署触发器Continuous deployment triggers
  • 添加阶段Adding stages
  • 添加预先部署审批Adding pre-deployment approvals
  • 创建发布和监视部署Creating releases and monitoring deployments

先决条件Prerequisites

需要:You'll need:

  • 包含至少一个阶段的发布管道。A release pipeline that contains at least one stage. 如果还没有,可以通过使用以下任一快速入门和教程来创建它:If you don't already have one, you can create it by working through any of the following quickstarts and tutorials:

  • 要在其中部署应用的两个不同的目标。Two separate targets where you will deploy the app. 这些是虚拟机、web 服务器、本地物理部署组或其他类型的部署目标。These could be virtual machines, web servers, on-premises physical deployment groups, or other types of deployment target. 在此示例中,我们使用 Azure 应用 Services 网站实例。In this example, we are using Azure App Services website instances. 如果你决定执行相同的操作,则必须选择唯一的名称,但最好将 "QA" 包含在名称中,将 "生产" 包含在另一个名称中,以便可以轻松地识别它们。If you decide to do the same, you will have to choose names that are unique, but it's a good idea to include "QA" in the name of one, and "Production" in the name of the other so that you can easily identify them. 使用 Azure 门户创建新的 web 应用。Use the Azure portal to create a new web app.

连续部署 (CD) 触发器Continuous deployment (CD) triggers

启用连续部署触发器将指示管道在每次有新的生成时自动创建新发布。Enabling continuous deployment trigger will instruct the pipeline to automatically create a new release every time a new build is available.

  1. Azure Pipelines 中,打开 " 发布 " 选项卡。选择发布管道,然后选择 " 编辑"。In Azure Pipelines, open the Releases tab. Select your release pipeline select Edit.

    编辑发布 pipelin

  2. 选择 "项目" 部分中的 "连续部署触发器" 图标,打开 "触发器" 面板。Select the Continuous deployment trigger icon in the Artifacts section to open the trigger panel. 请确保已启用此功能,以便在每次新的成功生成完成后创建新版本。Make sure this is enabled so that a new release is created after every new successful build is completed.

    持续部署触发器

  3. 在 "阶段" 部分选择 "预先部署条件" 图标以打开 "条件" 面板。Select the Pre-deployment conditions icon in the Stages section to open the conditions panel. 请确保将此阶段的部署触发器设置为 " 发布后"。Make sure that the trigger for deployment to this stage is set to After release. 这意味着在从此发布管道创建新版本时,将自动启动部署。This means that a deployment will be initiated automatically when a new release is created from this release pipeline.

    预先部署条件

    还可以设置 发布触发器暂存触发器计划部署You can also set up Release triggers, Stage triggers or schedule deployments.

添加阶段Add stages

在本部分中,我们将在本示例中将两个新阶段添加到发布管道: QA 和生产 (两个 Azure 应用服务网站) 。In this section, we will add two new stages to our release pipeline: QA and production (Two Azure App Services websites in this example). 这是一种典型的方案,在此方案中,你将最初部署到测试服务器或过渡服务器,然后部署到实时或生产服务器。This is a typical scenario where you would deploy initially to a test or staging server, and then to a live or production server. 每个 阶段 表示一个部署目标。Each stage represents one deployment target.

  1. 在发布管道中选择 " 管道 " 选项卡,然后选择 "现有" 阶段。Select the Pipeline tab in your release pipeline and select the existing stage. 将阶段的名称更改为 " 生产"。Change the name of your stage to Production.

    从 "管道" 选项卡中选择一个现有阶段,然后在 "阶段" 面板中将该名称更改为 "生产"

  2. 选择 " + 添加 " 下拉列表,然后选择 " 克隆阶段 (克隆选项仅在) 选择现有阶段时可用。Select the + Add drop-down list and choose Clone stage (the clone option is available only when an existing stage is selected).

    选择克隆阶段

    通常,您希望将相同的部署方法用于测试和生产阶段,以便您可以确保已部署的应用程序的行为方式相同。Typically, you want to use the same deployment methods with a test and a production stage so that you can be sure your deployed apps will behave the same way. 克隆现有阶段是确保对两者都具有相同设置的一种好方法。Cloning an existing stage is a good way to ensure you have the same settings for both. 然后,只需更改部署目标。You then just need to change the deployment targets.

  3. 克隆的阶段将具有 生产的名称副本Your cloned stage will have the name Copy of Production. 选择它并将名称更改为 " QA"。Select it and change the name to QA.

    将阶段名称更改为 QA

  4. 若要重新组织管道中的阶段,请选择 QA 阶段中的 "预先部署条件" 图标,并将触发器设置为 "发布后"。To reorganize the stages in the pipeline, select the Pre-deployment conditions icon in your QA stage and set the trigger to After release. 然后,管道图会并行显示两个阶段。The pipeline diagram will then show the two stages in parallel.

    重新组织阶段

  5. 选择 "生产" 阶段中的 "预先部署条件" 图标,将 "触发器" 设置为 "完成后",然后在 "阶段" 下拉列表中选择 "问答"。Select the Pre-deployment conditions icon in your Production stage and set the trigger to After stage, then select QA in the Stages drop-down list. 现在,管道关系图将指示将按正确的顺序执行两个阶段。The pipeline diagram will now indicate that the two stages will execute in the correct order.

    选择 QA 触发器和阶段

    备注

    可以将部署设置为在上一阶段的部署 部分 成功时启动。You can set up your deployment to start when a deployment to the previous stage is partially successful. 这意味着即使特定的非关键任务失败,部署也将继续。This means that the deployment will continue even if a specific non-critical task have failed. 这通常用于分叉和联接部署到不同阶段的部署。This is usually used in a fork and join deployments that deploy to different stages in parallel.

  6. 选择 " 任务 " 下拉列表,然后选择 " QA " 阶段。Select the Tasks drop-down list and select the QA stage.

    任务下拉,然后选择 "QA 阶段"

  7. 根据所使用的任务,更改设置,使此阶段部署到 "QA" 目标。Depending on the tasks that you are using, change the settings so that this stage deploys to your "QA" target. 在我们的示例中,我们将使用 " 部署 Azure App Service " 任务,如下所示。In our example, we will be using Deploy Azure App Service task as shown below.

    使用 "部署 azure 应用服务" 任务

添加预先部署审批Add Pre-deployment approvals

我们之前修改的发布管道部署到 QA 和生产环境中。The release pipeline we previously modified deploys to QA and production. 如果部署到 QA 失败,则不会触发部署到生产环境。If the deployment to QA fails, then deployment to production won't trigger. 建议始终在部署到生产环境之前,先验证应用是否在 QA 或测试阶段运行正常。It is recommended to always verify if your app is working properly in QA or test stage before deploying to production. 在部署到下一阶段之前,添加审批将确保满足所有条件。Adding approvals will ensure all the criteria are met before deploying to the next stage. 若要将审批添加到管道,请遵循以下步骤:To add approvals to your pipeline follow the steps below:

  1. 依次选择 " 管道 " 选项卡、" 预先部署条件 " 图标和 预先部署审批者Select the Pipeline tab, Pre-deployment conditions icon then Pre-deployment approvers.

    预先部署审批者面板

  2. 在 " 审批者 " 文本框中,输入将负责批准部署的用户 () 。In the Approvers text box, enter the user(s) that will be responsible for approving the deployment. 还建议取消选中 " 请求发布或部署的用户不应批准该用户 " 复选框。It is also recommended to uncheck the The user requesting a release or deployment should not approve it check box.

    添加预先部署审批者

    你可以根据需要添加多个审批者,包括单个用户和组织组。You can add as many approvers as you need, both individual users and organization groups. 还可以通过选择 "管道" 图中阶段右侧的 "用户" 图标来设置部署后审批。It's also possible to set up post-deployment approvals by selecting the "user" icon at the right side of the stage in the pipeline diagram. 有关详细信息,请参阅 发布入口和审批For more information, see Releases gates and approvals.

  3. 选择“保存”。Select Save.

    正在保存发布管道

创建发布Create a release

发布管道设置完成后,便可以开始部署了。Now that the release pipeline setup is complete, it's time to start the deployment. 为此,我们将手动创建一个新版本。To do this, we will manually create a new release. 通常,在新的生成项目可用时,会自动创建发布。Usually a release is created automatically when a new build artifact is available. 但是,在此方案中,我们将手动创建。However, in this scenario we will create it manually.

  1. 选择 " 发布 " 下拉列表,然后选择 " 创建发布"。Select the Release drop-down list and choose Create release.

    创建新版本

  2. 输入发布的说明,检查是否选择了正确的项目,然后选择 " 创建"。Enter a description for your release, check that the correct artifacts are selected, and then select Create.

    创建新的发布面板

  3. 将显示一个横幅,指示已创建新版本。A banner will appear indicating that a new release has been create. 选择 "发布" 链接以查看更多详细信息。Select the release link to see more details.

    已成功创建发布

  4. "发布摘要" 页将显示每个阶段的部署状态。The release summary page will show the status of the deployment to each stage.

    部署状态

    其他视图(例如发布列表)也会显示一个图标,指示正在等待审批。Other views, such as the list of releases, also display an icon that indicates approval is pending. 该图标将显示一个包含阶段名称的弹出窗口,并在指向它时显示更多详细信息。The icon shows a pop-up containing the stage name and more details when you point to it. 管理员可以凭此轻松查看哪些发布正在等待审批,以及所有发布的总体进度。This makes it easy for an administrator to see which releases are awaiting approval, as well as the overall progress of all releases.

    发布列表视图

  5. 选择 " pending_approval " 图标以打开 "批准" 窗口面板。Select the pending_approval icon to open the approval window panel. 输入简短注释,然后选择 " 批准"。Enter a brief comment, and select Approve.

    批准部署

备注

你可以计划在以后的某个时间进行部署,例如在非高峰时段。You can schedule deployment at a later date, for example during non-peak hours. 你还可以向其他用户重新分配批准。You can also reassign approval to a different user. 发布管理员可以访问并覆盖所有批准决定。Release administrators can access and override all approval decisions.

监视和跟踪部署Monitor and track deployments

部署日志可帮助你监视和调试应用程序的版本。Deployment logs help you monitor and debug the release of your application. 若要检查部署的日志,请执行以下步骤:To check the logs of our deployment follow the steps below:

  1. 在 "发布摘要" 中,将鼠标悬停在某个阶段上,然后选择 " 日志"。In the release summary, hover over a stage and select Logs.

    部署日志

    在部署期间,仍可以访问 "日志" 页,查看每个任务的实时日志。During deployment, you can still access the logs page to see the live logs of every task.

  2. 选择任何任务以查看此特定任务的日志。Select any task to see the logs for that specific task. 这样可以更轻松地跟踪和调试部署问题。This makes it easier to trace and debug deployment issues. 你还可以下载单个任务日志或所有日志文件的 zip。You can also download individual task logs, or a zip of all the log files.

    下载日志

  3. 如果需要其他信息来调试部署,可以 在调试模式下运行发布If you need additional information to debug your deployment, you can run the release in debug mode.

后续步骤Next step