Nico
Nico

Reputation: 719

Azure DevOps Server - YAML Pipeline condition retried jobs

We are using Azure DevOps Server 2020 and are using YAML pipelines.

Due to the lack of the auto-retry functionality in ADS 2020, i have built my own. I added a stage at the end of my pipeline which has the failed() condition. So whenever a job fails, the retry stage will kick in, which will trigger a retry build that tries the failed jobs of the provided buildId. This is working so far.

One enhancement to this that would be nice, is that i would be able to see from the build list, which builds have gone through this auto-retry. What happens is that when the auto-retry is run, the failing job will retry, but because the retry stage at the end depends on the failed job, it will also retry. Assuming the failed job succeeds the second time, the retry job will not run again and will have a final state of skipped.

image

What i would like to achieve is that the retry job will run on 2 conditions. Failed() and something like "has retried".

Does anyone have an idea how to achieve this? I thought about adding a tag to the build and condition on that, but i dont think that is possible. I could add a variable to the pipeline the first time im in the retry stage, but i dont think that variable is available the second round.

Is there any value i could condition on that could help me identify one of the jobs in the pipeline had a second attempt?

Solution: Thanks to the comments made by Bright, i have configured the following condition on my final stage

condition: or(failed(), gt(variables['System.StageAttempt'], 1)) 

Upvotes: 1

Views: 1241

Answers (1)

Bright Ran-MSFT
Bright Ran-MSFT

Reputation: 14184

There is a predefined variable 'System.JobAttempt' which is used to record the number of times that a job has ran / retried.

enter image description here

Similarly, the predefined variable 'System.StageAttempt' is used to record the number of times that a stage has ran / retried.

enter image description here

You can use one of these two predefined variables on the conditions of the retry stage. It should be able to meet your demands.

For more details about predefined variables, you can see the document "Use predefined variables".

And reference the following documents to learn how to use variables on conditions.

Upvotes: 1

Related Questions