soydachi
soydachi

Reputation: 901

Yaml multi stage run using dependson and conditions

I have a need to run the "Secure" stage if one of the previous stages of INT was sucessfully passed. I tried with dependson and conditions but I can't find the solution.

enter image description here

Upvotes: 8

Views: 19006

Answers (2)

Jay Really
Jay Really

Reputation: 95

I similar cases, I use the actual result status variables of all it's depending stages. Here, you can build quite complicated logic if necessary because there are more options than the default functions offer.

So if you want the Secure stage to run if any (one or more) of it's predecessors ran (successfully), then you could do something like:

  - stage: Secure
    dependsOn: 
      - stage_A  
      - stage_B
    condition: |
      and (
        in(dependencies.stage_A.result, 'Succeeded', SucceededWithIssues', 'Skipped'),
        in(dependencies.stage_B.result, 'Succeeded', SucceededWithIssues', 'Skipped'),
        not (
            eq(dependencies.stage_A.result, 'Skipped'),
            eq(dependencies.stage_B.result, 'Skipped')
        )
      )

In the above example, the Secure stage would run if all the depending stages either ran successfully or did not run at all, except if none of them ran.

Hope this helps!

Upvotes: 1

Leo Liu
Leo Liu

Reputation: 76660

I have a need to run the "Secure" stage if one of the previous stages of INT was sucessfully passed.

I am afraid there is no such out of YAML syntax to achieve this at this moment.

Since we need to set multiple depend on for the stage Secure:

- stage: Deploy
  dependsOn:
    - INT_API
    - INT_FuncIntergration
    - INT_Web
  condition: or(succeeded('INT_API'), succeeded('INT_FuncIntergration'), succeeded('INT_Web'))

Restriction:

This method can only be used the previous stage has a success, then this stage will be executed, but the current stage needs to be executed after all the previous stages have been executed. If you need to execute the current stage as long as one of the previous stages is successful, this method is still not enough.

That is because there is no "OR" syntax for the depend on. And we could not add the condition for the depend on, like:

 - stage: Deploy
   ${{ if eq(result.INT_API, successed) }}:
    dependsOn:
      - INT_API
      - INT_FuncIntergration
      - INT_Web
    condition: or(succeeded('INT_API'), succeeded('INT_FuncIntergration'), succeeded('INT_Web'))

Because the condition is parsed when YAML is compiled, but at this time the running result of the previous stage has not yet come out.

You could submit this request condition "OR" to our UserVoice site (https://developercommunity.visualstudio.com/content/idea/post.html?space=21 ), which is our main forum for product suggestions. Thank you for helping us build a better Azure DevOps.

Workaround:

The main idea of the solution is: You could try to set depend on for the stage Secure with [], then add a Inline powershell task before other tasks. This task will call the REST API Definitions - Get to monitor whether all the stages in the current release pipeline have inprocess and queue states. If so, wait for 30 seconds, and then loop again until all other stages in the current release pipeline have no inprocess and queue states. Then next execute other tasks will be executed.

You could check my previous ticket for detailed info:

Upvotes: 8

Related Questions