向发布管道添加连续监视Add continuous monitoring to your release pipeline

Azure Pipelines 与 Azure Application Insights 集成,可连续监视整个软件开发生命周期内的 DevOps 发布管道。Azure Pipelines integrates with Azure Application Insights to allow continuous monitoring of your DevOps release pipeline throughout the software development lifecycle.

借助持续监视,发布管道可以结合来自 Application Insights 和其他 Azure 资源的监视数据。With continuous monitoring, release pipelines can incorporate monitoring data from Application Insights and other Azure resources. 当发布管道检测到 Application Insights 警报时,该管道可以限制或回滚部署,直到警报已解决。When the release pipeline detects an Application Insights alert, the pipeline can gate or roll back the deployment until the alert is resolved. 如果所有检查都通过,可以从测试到生产全程自动执行部署,无需手动干预。If all checks pass, deployments can proceed automatically from test all the way to production, without the need for manual intervention.

配置连续监视Configure continuous monitoring

  1. Azure DevOps 中选择一个组织和项目。In Azure DevOps, select an organization and project.

  2. 在项目页的左菜单中,选择“管道” > “发布”。 On the left menu of the project page, select Pipelines > Releases.

  3. 单击“新建”旁边的下拉箭头,然后选择“新建发布管道”。 Drop down the arrow next to New and select New release pipeline. 如果你没有管道,请在显示的页面上选择“新建管道”。Or, if you don't have a pipeline yet, select New pipeline on the page that appears.

  4. 在“选择模板”窗格中,搜索并选择“支持持续监视的 Azure 应用服务部署”,然后选择“应用”。 On the Select a template pane, search for and select Azure App Service deployment with continuous monitoring, and then select Apply.

    新建 Azure Pipelines 发布管道

  5. 在“阶段 1”框中,选择“查看阶段任务”超链接。 In the Stage 1 box, select the hyperlink to View stage tasks.

    查看阶段任务

  6. 在“阶段 1”配置窗格中填写以下字段:In the Stage 1 configuration pane, complete the following fields:

    参数Parameter ValueValue
    阶段名称Stage name 提供阶段名称,或将其保留为“阶段 1”。Provide a stage name, or leave it at Stage 1.
    Azure 订阅Azure subscription 单击下拉箭头并选择要使用的 Azure 链接订阅。Drop down and select the linked Azure subscription you want to use.
    应用类型App type 单击下拉箭头并选择应用类型。Drop down and select your app type.
    应用服务名称App Service name 输入 Azure 应用服务的名称。Enter the name of your Azure App Service.
    Application Insights 的资源组名称Resource Group name for Application Insights 单击下拉箭头并选择要使用的资源组。Drop down and select the resource group you want to use.
    Application Insights 资源名称Application Insights resource name 单击下拉箭头并选择所选资源组的 Application Insights 资源。Drop down and select the Application Insights resource for the resource group you selected.
  7. 若要使用默认的警报规则设置保存管道,请在“Azure DevOps”窗口的右上角选择“保存”。To save the pipeline with default alert rule settings, select Save at upper right in the Azure DevOps window. 输入描述性的注释,然后选择“确定”。Enter a descriptive comment, and then select OK.

修改警报规则Modify alert rules

现成的“支持持续监视的 Azure 应用服务部署”模板包含四个警报规则:“可用性”、“失败的请求”、“服务器响应时间”和“服务器异常”。 Out of box, the Azure App Service deployment with continuous monitoring template has four alert rules: Availability, Failed requests, Server response time, and Server exceptions. 可以添加更多规则,或根据服务级别需求更改规则设置。You can add more rules, or change the rule settings to meet your service level needs.

若要修改警报规则设置:To modify alert rule settings:

在发布管道页的左窗格中,选择“配置 Application Insights 警报”。In the left pane of the release pipeline page, select Configure Application Insights Alerts.

四个默认警报规则是通过内联脚本创建的:The four default alert rules are created via an Inline script:

$subscription = az account show --query "id";$subscription.Trim("`"");$resource="/subscriptions/$subscription/resourcegroups/"+"$(Parameters.AppInsightsResourceGroupName)"+"/providers/microsoft.insights/components/" + "$(Parameters.ApplicationInsightsResourceName)";
az monitor metrics alert create -n 'Availability_$(Release.DefinitionName)' -g $(Parameters.AppInsightsResourceGroupName) --scopes $resource --condition 'avg availabilityResults/availabilityPercentage < 99' --description "created from Azure DevOps";
az monitor metrics alert create -n 'FailedRequests_$(Release.DefinitionName)' -g $(Parameters.AppInsightsResourceGroupName) --scopes $resource --condition 'count requests/failed > 5' --description "created from Azure DevOps";
az monitor metrics alert create -n 'ServerResponseTime_$(Release.DefinitionName)' -g $(Parameters.AppInsightsResourceGroupName) --scopes $resource --condition 'avg requests/duration > 5' --description "created from Azure DevOps";
az monitor metrics alert create -n 'ServerExceptions_$(Release.DefinitionName)' -g $(Parameters.AppInsightsResourceGroupName) --scopes $resource --condition 'count exceptions/server > 5' --description "created from Azure DevOps";

可以修改脚本并添加其他警报规则、修改警报条件或删除对部署没有意义的警报规则。You can modify the script and add additional alert rules, modify the alert conditions, or remove alert rules that don't make sense for your deployment purposes.

添加部署条件Add deployment conditions

将部署门限添加到发布管道时,会出现一条指出超过了设置的阈值的警报,此警报旨在防止不需要的发布升级。When you add deployment gates to your release pipeline, an alert that exceeds the thresholds you set prevents unwanted release promotion. 解决该警报后,部署可自动继续。Once you resolve the alert, the deployment can proceed automatically.

若要添加部署门限:To add deployment gates:

  1. 在管道主页上的“阶段”下,根据哪个阶段需要持续监视门限,选择“部署前的条件”或“部署后的条件”符号。 On the main pipeline page, under Stages, select the Pre-deployment conditions or Post-deployment conditions symbol, depending on which stage needs a continuous monitoring gate.

    部署前的条件

  2. 在“部署前的条件”配置窗格中,将“门限”设置为“已启用”。 In the Pre-deployment conditions configuration pane, set Gates to Enabled.

  3. 在“部署门限”的旁边,选择“添加”。 Next to Deployment gates, select Add.

  4. 从下拉菜单中选择“查询 Azure Monitor 警报”。Select Query Azure Monitor alerts from the dropdown menu. 使用此选项可以访问 Azure Monitor 和 Application Insights 警报。This option lets you access both Azure Monitor and Application Insights alerts.

    查询 Azure Monitor 警报

  5. 在“评估选项”下,输入要对“重新评估门限的间隔时间”和“门限失败之前所要经过的超时”等设置使用的值。 Under Evaluation options, enter the values you want for settings like The time between re-evaluation of gates and The timeout after which gates fail.

查看发布日志View release logs

可以在发布日志中查看部署门限行为和其他发布步骤。You can see deployment gate behavior and other release steps in the release logs. 若要打开日志:To open the logs:

  1. 从管道页的左菜单中选择“发布”。Select Releases from the left menu of the pipeline page.

  2. 选择任一发布活动。Select any release.

  3. 在“阶段”下,选择任一阶段以查看发布摘要。Under Stages, select any stage to view a release summary.

  4. 若要查看日志,请在发布摘要中选择“查看日志”,然后在任一阶段中选择“成功”或“失败”超链接,或者将鼠标悬停在任一阶段上并选择“日志”。 To view logs, select View logs in the release summary, select the Succeeded or Failed hyperlink in any stage, or hover over any stage and select Logs.

    查看发布日志

后续步骤Next steps

有关 Azure Pipelines 的详细信息,请参阅 Azure Pipelines 文档For more information about Azure Pipelines, see the Azure Pipelines documentation.