1

I have a release pipeline which is triggered by CI where I want to push a new version of a nuget package if that version does not exist yet.

For that I have a simple command line task which checks whether that nuget is present on nuget.org and stores the result in an environment variable.

I can then set the condition for next steps which will only execute the step if that value variable is true.

enter image description here

This works all fine, however I wonder how can I simply stop the release process and skip all subsequent steps, without setting their condition one by one.

Also, to that, how can I set the build status to 'Cancelled' by a task?

Bartosz
  • 4,406
  • 7
  • 41
  • 80

1 Answers1

5

You can cancel current Build or Release with a PowerShell task:

Write-Host "##vso[task.setvariable variable=agent.jobstatus;]canceled"
Write-Host "##vso[task.complete result=Canceled;]DONE"

So in the release pipeline add a Powershell task with the above code and in the custom condition configure it to run only if you don't want to upload the NuGet, after this task all the tasks after it will be canceled.

PS - The status will be "Succeeded" but in fact the Build/Release will be canceled.

If you want must to see the status "Canceled" you need to use Rest API, check this PowerShell script.

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
  • 2
    I tried these two lines, but got this: `##[warning]Overwriting readonly task variable 'agent.jobstatus'. This behavior will be disabled in the future. See https://github.com/microsoft/azure-pipelines-yaml/blob/master/design/readonly-variables.md for details.` – Will Huang Feb 18 '20 at 03:44
  • @WillHuang this is interesting, look like a new warning from Microsoft. anyway, I think we can use only the second line `Write-Host "##vso[task.complete result=Canceled;]DONE"` – Shayki Abramczyk Feb 18 '20 at 13:52
  • 2
    I tried only second line too.. But no luck, the build continues. – Tolga Arican Jun 25 '20 at 13:23
  • 1
    @ShaykiAbramczyk, will this only cancel the running task, not the whole pipeline? Are there different commands for tasks, jobs and stages? – zameb Aug 24 '20 at 08:30
  • Not relevant with current Azure DevOps Server 2020 rc2 [error]Overwriting readonly variable 'agent.jobstatus' is not permitted. See https://github.com/microsoft/azure-pipelines-yaml/blob/master/design/readonly-variables.md for details. – galsi Sep 08 '20 at 11:24
  • 1
    @galsi try only the second line: `Write-Host "##vso[task.complete result=Canceled;]DONE"` – Shayki Abramczyk Sep 08 '20 at 13:44
  • 1
    i tried again with 2020 RC2 and using only this line task is cancelled but build continue – galsi Sep 13 '20 at 09:49
  • Thanks @galsi, so you must use rest api. see here: https://stackoverflow.com/questions/62044055/is-it-possible-to-cancel-a-azure-devops-pipeline-job-programmatically – Shayki Abramczyk Sep 14 '20 at 06:11
  • 1
    also a problem with RestAPI - see my post at DeveloperCommunity https://developercommunity.visualstudio.com/content/problem/1182933/build-cancel-with-restapi-contine-to-run-next-task.html – galsi Sep 15 '20 at 11:05