2

I am using classic editor release pipeline and the requirement is to run a 'send email' task when a specific task fails. How can we configure custom condition for this requirement?

When we use the Azure provided condition 'Only when a previous task has failed', the task is getting executed if any of the previous task has failed. Hence, requirement is not met. ie if there are three tasks A,B,C and the requirement is to trigger C only when B fails, using 'Only when a previous task has failed' as condition will trigger C even when A fails.

Please help with the steps to configure custom condition for this requirement.

enter image description here

1

4 Answers 4

1
+50

As a workaround:

  1. Add a new variable myspecificstepfails with default value YES

  2. Add after your specific step bash or PowerShell task: SetVariable: Initialize or modify the value of a variable

    - bash: |
        echo "##vso[task.setvariable variable=myspecificstepfails;]NO"
    
  3. Add the custom condition to your send email

    condition: eq(variables['myspecificstepfails'], 'YES'))
    
Sign up to request clarification or add additional context in comments.

Comments

0

In the "Custom conditions" field, enter the condition that checks if the previous task failed:
For example, you can use the expression eq(variables['Agent.JobStatus'], 'Failed') to check if the previous task failed.

Then save your changes.

Related question?

1 Comment

And the answer to question ”How do I check if specific (not necessarily immediately preceding) task failed" is?
0

You could get the status through powershell from the api and send email when it's failed. Ensure to get a unique title for the task you want to monitor.

$url = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds/$(Build.BuildId)/timeline?api-version=5.1"

$result = Invoke-RestMethod -Uri $url -Headers @{authorization = "Bearer $(system.accesstoken)"} -ContentType "application/json" -Method get

$taskResult = $result.records | where {$_.name -eq "UnitTest"} | select result

echo $taskResult 

Source for answer: https://stackoverflow.com/a/60611680/12927762

2 Comments

does this work even when the pipeline is not finished yet (during release)?
It should, the information needs to be stored during run as well. And it's passing the building of the current run.
0

The powershell to roll back task has a way to download the job status and you can use it to look at the status of all individual tasks in a job. I'm not 100% sure this works with the most recent iterations of the Azure Pipelines, but it sure is a way to start:

https://marketplace.visualstudio.com/items?itemName=ms-devlabs.utilitytasks

try
{
   $jsonobject = ConvertFrom-Json $env:Release_Tasks
}
catch
{
   Write-Verbose -Verbose "Error converting from json"
   Write-Verbose -Verbose $Error
}


foreach ($task in $jsonobject | Get-Member -MemberType NoteProperty) {
   $taskproperty = $jsonobject.$($task.Name) | ConvertFrom-Json
   Write-Verbose -Verbose "Task $($taskproperty.Name) with rank $($task.Name) has status $($taskproperty.Status)"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question