Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute task when build is cancelled - Azure DevOps Pipelines

I need my pipeline to run a task if the build is cancelled either by a user or automatically (by the system itself).

Is there any condition to do so?

Thanks!

like image 513
nunolima41 Avatar asked Oct 21 '25 16:10

nunolima41


2 Answers

Yes, you can use always()

steps:
- script: echo I did a thing
  condition: and(always(), eq(someConditionHere))

More on that here: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/conditions?view=azure-devops&tabs=yaml

like image 121
David Petric Avatar answered Oct 23 '25 08:10

David Petric


If you need the task to run only when the build is canceled, you need to check both always() and succeededOrFailed().

Because succeededOrFailed() is false when the pipeline is canceled, the following condition works:

- script: |
    echo "Pipeline was canceled."
  condition: and(always(), eq(succeededOrFailed(), false))
like image 30
colinD Avatar answered Oct 23 '25 07:10

colinD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!