Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

How to set an azure devops pipeline result as success even if one of the job fails

I am developing an Azure CD YAML pipeline to deploy the result of a CI pipeline onto a Virtual Machine. Right now and simplifying things a little for the purpose of this post, the CD pipeline is quite simple and consist of a single stage with 3 jobs:

  • The first job runs scripts to stop a somehow complex applications. This can sometimes fail.
  • The second job will only run if first job fails. This to give the opportunity for an administrator to do a manual intervention (leveraging the built-in 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

enter image description here

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...