【问题标题】:Find out which job and task failed in an Azure pipeline run?找出 Azure 管道运行中哪个作业和任务失败?
【发布时间】:2022-08-10 13:43:47
【问题描述】:

我正在运行多个 Azure 管道,每个管道都包含一个包含多个作业的版本,其中有多个任务。 如果管道失败,我想知道,在管道运行期间,哪个作业中的哪个任务失败了。 我在 Microsoft Teams 频道中有一个服务挂钩,我想向其发送通知。 假设我添加了一个任务,该任务仅在前一个任务失败时运行,在此任务中,我想发送有关管道故障的 Teams 通知。 有没有办法让我在这个工作/任务中知道管道在哪里中断(无需使用此信息设置管道变量值,因为我有超过 20 个管道(并且还在增长),每个管道有超过 6 个作业和 100 个任务)?

标签: azure-devops azure-pipelines


【解决方案1】:

根据您的描述,您可以尝试在 Teams 频道中使用自定义卡片 Create and send messages。请参考以下简要步骤。

  1. 添加和配置传入 Webhook 连接器;

    1. 复制出Webhook Url并将其定义为 下面的变量$(UrlGeneratedFromIncomingWebhookConnector) 示例 YAML 管道;它在作业结束时使用 PowerShell 任务 并且任务将在 目标 Teams 频道,前提是之前的任务失败。
        trigger:
          branches:
            include:
            - main
          paths:
            include:
            - SendNotificationThroughIncomingWebhook.yml
        pool:
          vmImage: ubuntu-latest
        jobs:
        - job: Job_0
          steps:
          - checkout: none
          - script: echo Hello, world!
            displayName: 'Job_0_Succeeded'
        - job: Job_1
          dependsOn: Job_0
          steps:
            - checkout: none
            - script: |
                Write-Host "##vso[task.logissue type=error]Something went very wrong."
                exit 1
              displayName: 'Simulate a failed task'
            - task: PowerShell@2
              displayName: 'Send Notification Through Incoming Webhook'
              condition: failed()
              inputs:
                targetType: 'inline'
                script: |
                  $URL = '$(UrlGeneratedFromIncomingWebhookConnector)'
                  $header = @{
                  'Content-Type' = 'application/json'
                  }
                  
                  $body = @"
                  {
                      "@type": "MessageCard",
                      "@context": "http://schema.org/extensions",
                      "themeColor": "0076D7",
                      "summary": "CI Pipeline Notification",
                      "sections": [
                          {
                              "activityTitle": "$(Build.DefinitionName) - $(Build.BuildNumber)",
                              "activitySubtitle": "status : $(Agent.JobStatus)",
                              "activityImage": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTy5ihdbdBvYPJ6cEoJFkF6ED9I7LnoaP22yg&usqp=CAU",
                              "facts": [
                                  {
                                      "name": "Build",
                                      "value": "$(Build.DefinitionName) - $(Build.BuildNumber)"
                                  },
                                  {
                                          "name": "Job Name",
                                          "value": "$(Agent.JobName)"
                                  },
                                  {
                                      "name": "Repository",
                                      "value": "$(Build.Repository.Name)"
                                  },
                                  {
                                      "name": "Branch",
                                      "value": "$(Build.SourceBranchName)"
                                  },
                                  {
                                      "name": "Commit",
                                      "value": "$(Build.SourceVersionMessage)"
                                  },
                                  {
                                      "name": "Requested For:",
                                      "value": "$(Build.RequestedFor)"
                                  },
                                  {
                                      "name": "Date & Time:",
                                      "value": "$(system.pipelineStartTime)"
                                  }
                              ],
                              "markdown": true
                          }
                      ],
                      "potentialAction": [
                          {
                              "@type": "OpenUri",
                              "name": "View Pipeline Run",
                              "targets": [
                                  {
                                      "os": "default",
                                      "uri": "$(System.CollectionUri)/$(System.TeamProject)/_build/results?buildId=$(Build.BuildId)&view=results"
                                  }
                              ]
                          },
                          {
                              "@type": "OpenUri",
                              "name": "View Commit",
                              "targets": [
                                  {
                                      "os": "default",
                                      "uri": "$(System.CollectionUri)$(System.TeamProject)/_git/$(Build.Repository.Name)/commit/$(Build.SourceVersion)?refName=refs%2Fheads%2F$(Build.SourceBranchName)"
                                  }
                              ]
                          }
                      ]
                  }
                  "@ 
                  Invoke-RestMethod -Method Post -Uri $URL -Headers $header -Body $body | ConvertTo-Json
    
    
    1. 管道将模拟失败的任务并发送如下消息 在您的 Teams 频道下方;
    2. 更多信息,请参考以下文档; Create an Incoming Webhook - Teams | Microsoft Docs Create and send messages - Teams | Microsoft Docs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-30
    • 2021-05-10
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    • 2022-07-15
    • 1970-01-01
    相关资源
    最近更新 更多