Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSTS - Is there a way to only run a task if a specific task has failed?

Tags:

azure-devops

I need to run a publish task in my build definition but only if a certain task before it has failed. If the other task passes I want this to be ignored and not run.

Is there a way of doing this?

I was hoping that I could set an output variable based on the task success and then use that variable in a custom condition to run the task if it's failed.

I can't see how to set an output variable if the task fails. Is this possible?

like image 482
darren25 Avatar asked Sep 13 '18 10:09

darren25


People also ask

How do you add multiple conditions in Azure pipeline?

You could add two same tasks in the pipeline, one with the condition ((Var1==A || Var1==B || Var1==C) && (Var2==2)) and another with condition ((Var1==A) &&(Var2==1)) , this should be work.

How do I disable a task in Azure Devops pipeline?

You can try set variables : enabled = false , then use the variable in YAML file. If set this way, this task will not run in the job. This method is mentioned in this case, you can refer to it for details.

How do you pass variables between tasks in Azure Devops?

Share variables between Tasks across the Jobs (of the same Stage) We need to use the isOutput=true flag when you desire to use the variable in another Task located in another Job. Navigate to Stage1_Job1_Task1 and add isoutput = true flag to the Logging Command which let's us to access the value outside the Job.


1 Answers

  • In the Task settings expand the "Control Options"
  • Choose in "Run this task" - Only when a previous task has failed.

enter image description here

If you want to run the task only if last/specific task failed:

Suppose your specific task (the one you examine in regards to its status) is called A. The goal is to call another build task (let's say B) only in case A fails.

  • Define a custom build variable, call it task.A.status and set to success
  • Create another build task, e.g. C and schedule it right after A; condition it to only run if A fails - there's a standard condition for that
  • The task C should only do one thing - set task.A.status build variable to 'failure' (like this, if we are talking PowerShell: Write-Host "##vso[task.setvariable variable=task.A.status]failure")
  • Finally, the task B is scheduled sometime after C and is conditioned to run in case task.A.status equals failure, like this: eq(variables['task.A.status'], 'failure')

enter image description here

like image 94
Shayki Abramczyk Avatar answered Sep 23 '22 12:09

Shayki Abramczyk