Andree
Andree

Reputation: 1199

Azure DevOps: Custom condition on task to be run if anything failed

I want to send the notification if anything in the stage fails. How can I set the condition in the Release pipeline's task so the task is run when anything previously failed?

The marked option works only for the immediate predecessor. enter image description here

But consider a situation there is a task which failed, then there is a task which will be run always and after that one is the task I want to run if anything fails.

Upvotes: 1

Views: 12060

Answers (3)

Everton Oliveira
Everton Oliveira

Reputation: 950

This is an old thread but in case someone is facing the same issue, I managed to achieve the condition "if anything fails" by using the predefined variable [Agent.JobStatus]. It has the following statuses :

  1. Canceled
  2. Failed
  3. Succeeded
  4. SucceededWithIssues (partially successful)

Having the values above I set the following custom condition:

eq(variables['Agent.JobStatus'], 'SucceededWithIssues')

If you wish to have multiple custom conditions, you can use logical operators as follows :

or(eq(variables['Agent.JobStatus'], 'Canceled'),eq(variables['Agent.JobStatus'], 'Failed'),eq(variables['Agent.JobStatus'], 'SucceededWithIssues'))

Azure Devops custom condition

Result:

enter image description here

reference:https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml#agent-variables

Upvotes: 6

ccoutinho
ccoutinho

Reputation: 4556

As mentioned in the other answer, you need to use a custom condition and set it to failed(). But that alone won't do the trick. I believe you could achieve what you want by listing all the previous jobs being run before the Notification job in its dependsOn field. That way, if any of the listed jobs fails, your Notification job will be executed.

jobs:
- job: Build
  (...)
- job: CleanUp
  (...)
- job: Notification
      dependsOn: 
      - Build
      - Cleanup
      condition: failed()
      steps:
        - task: (...)

Upvotes: 0

4c74356b41
4c74356b41

Reputation: 72171

I think you need to use custom condition and set it to failed()

quote:

With no arguments, evaluates to True only if any previous job in the dependency graph failed.

Reading:
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/conditions?view=azure-devops&tabs=yaml#job-status-functions

Upvotes: 1

Related Questions