即使一项工作失败,如何将天蓝色的devops管道结果设置为成功 [英] How to set an azure devops pipeline result as success even if one of the job fails

查看:69
本文介绍了即使一项工作失败,如何将天蓝色的devops管道结果设置为成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Azure CD YAML管道,以将CI管道的结果部署到虚拟机上.现在,为了本文的目的而简化一些工作,CD管道非常简单,由一个阶段和3个工作组成:

  • 第一个作业运行脚本来停止某种复杂的应用程序.有时可能会失败.
  • 仅当第一个作业失败时,第二个作业才会运行.这为管理员提供了机会进行手动干预(利用内置的

    对于作业 ManualIntervation ,您还需要更改为 condition .

    然后更改作业 ManualIntervation 的条件,以检查flag变量是否设置为true.见下文:

     -作业:ManualIntervationDependOn:StopApplication条件:eq(dependencies.StopApplication.result,'SucceededWithIssues') 

    2,另一个解决方法是将 StopApplication 作业与其他管道中的其他作业分开.

    您需要创建两个管道.第一个管道仅具有 StopApplication 作业.第二个管道包含其余的作业.并使用

    然后在第二个管道中定义Manual Validation task) and fix the issue encountered in the first job. If the administrator is happy to continue to run the deployment pipeline, he will resume the run of the pipeline.

  • The third step is the deployment of the new version of the application.

Here is the overall structure of the YAML pipeline:

jobs:
  - deployment: StopApplication
    environment:
      name: 'EnvA'  # This environment is a set of virtual machines running self-hosted Azure Agents.
      resourceType: VirtualMachine
    strategy:
          rolling:
            maxParallel: 1
            deploy:
              steps:
              - task: ...
  - job: ManualIntervation
        displayName: Manual intervention to fix issue while stopping application
        pool: server
        dependsOn: StopApplication
        condition: failed()  # This job will run only if job StopApplication has failed.
        timeoutInMinutes: 60 
        steps:
        - task: ManualValidation@0
          timeoutInMinutes: 50
          inputs:
            notifyUsers:
              someone@somewhere.com
            instructions: 'Do something'
            onTimeout: 'reject'
  - deployment: DeployApp
        dependsOn:
        - StopApplication
        - ManualIntervation
        condition: xor(succeeded('StopApplication'), succeeded('ManualIntervation'))
        workspace:
          clean: all
        environment:
          name: 'EnvA'  # This environment is a set of virtual machines running self-hosted Azure Agents.
          resourceType: VirtualMachine
        strategy:
          rolling:
            maxParallel: 1
            deploy:
              steps:
              - task: ...

The problem I have is that if the first deployment job fails but that the administrator review the problem, fixes it, resume the run of the pipeline and that the last deployment job succeeds, Azure DevOps shows my pipeline as Failed (red cross in the DevOps portal) which I can understand as one of the jobs failed. Nevertheless, functionally, the deployment succeeded and so I would like to set/force the result of the pipeline run as a success so that Azure DevOps display the green check.

Does anyone know the way to achieve this? I would assume that it is possible otherwise I would not understand why we have the opportunity for manual interventions in a pipeline.

解决方案

The build result is read-only and cannot be updated after the build is completed. However, You can check out below workarounds to get rid of the Failed sign(Red Cross) in Devops portal.

1, Use continueOnError for the task in StopApplication job. For below example:

jobs:
 - deployment: StopApplication
   ...
    steps:
    - task: taskName
       ...
      continueOnError: true

When the continueOnError attribute is set to true. The pipeline's result will be set to SucceededWithIssues when the task failed. You will have a exclamation mark instead of red Cross

You also need to change to the condition for job ManualIntervation.

Then change the condition for job ManualIntervation to check if the flag variable was set to true. See below:

- job: ManualIntervation
  dependsOn: StopApplication
  condition: eq(dependencies.StopApplication.result, 'SucceededWithIssues')

2, Another workaround is to separate the StopApplication job from the others jobs in a different pipeline.

You need to create two pipelines. The first pipeline only have StopApplication job. The second pipeline contains the rest of the jobs. And trigger the second pipeline from the first pipeline using rest api.

In the First pipeline. And a powershell task after the failed task to check if the job status and trigger the second pipeline using rest api. See below example:

 - powershell: |
      
      $body = @{
                templateParameters=@{
                    ManualIntervation= "false"
                }
              }

      if("$(Agent.JobStatus)" -eq "Failed"){
          $body.templateParameters.ManualIntervation='true'
      }
      $url = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/pipelines/{second-pipelineId}/runs?api-version=6.1-preview.1"
      $result5 = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $(system.accesstoken)"} -Method post -Body (convertto-json $body) -ContentType "application/json" 

    condition: always() #always run this task

Then in the second pipeline define a runtime parameter ManualIntervation and set the condition for job ManualIntervation see below:

parameters:
- name: ManualIntervation
  type: string
  default: false
  
...

- job: ManualIntervation
  dependsOn: StopApplication
  condition: eq('${{parameters.ManualIntervation}}', 'true') 

When the first pipeline is executed. The powershell task will be trigger the second pipeline will the template parameter request body to override the parameter ManualIntervation in the second pipeline. If the ManualIntervation is true. Then the ManualIntervation job will be executed.

So that the second pipeline will be succeeded even if the first pipeline failed.

这篇关于即使一项工作失败,如何将天蓝色的devops管道结果设置为成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆