azure-devopsazure-pipelines

Azure Pipelines: How to make a task dependent on a previous task?


I'm using Azure-Pipelines for my CI integration on windows-2019. Here's a scenario, the task #'s mean order in which they occur (1 being first).

In task 2, I am running tests. In task 3, I want to generate a report on these tests, regardless if the tests succeeded or failed (thus adding - condition: succeededOrFailed() to task 3).

However, in task 1, I am building the repo. If the build fails, I don't want any subsequent tasks to run. But since task 3 has condition: succeededOrFailed(), it still runs and produces another error. My goal is to have task 3 run regardless if task 2 fails or succeeds, but not if task 1 fails.

I'm not sure the best way to handle this. Is it possible to make task 3 dependent on task 2? Or can I stop the whole pipeline immediately if task 1 fails?

Also, for task 1, I tried continueOnError: false because I thought it would stop the pipeline there, but it didn't do what I thought. Any help would be much appreciated!


Solution

  • My goal is to have task 3 run regardless if task 2 fails or succeeds, but not if task 1 fails.

    From your requirement, you can connect task 3 and task 1.

    Here is the Yaml template:

    trigger:
    - master
    
    pool:
      vmImage: 'ubuntu-latest'
    
    steps:
    - task: task1
    ...
    
    
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          # Write your PowerShell commands here.
    
          Write-Host "##vso[task.setvariable variable=task.A.status]Success"      
      condition: succeeded()
    
    - task: task2
     ...
      condition: succeeded()
    
    - task: task 3
    ....
      condition: and (succeededOrFailed(), eq(variables['task.A.status'], 'success'))
    

    Explaination:

    The Powershell task is used to set an output variable for task 1. When task 1 success, the variable will set the value success. This variable could be used in the following tasks.

    The condition in task 2 depends on task 1.

    The condition in task 3 needs to meet two conditions at the same time. (1. no matter success or fail , 2. The custom variable value is success).

    Then the task 3 will run only when the task 1 success. And task 3 will run regardless if task 2 fails or succeeds.

    Hope this helps.