Can I cancel a TeamCity build from my msbuild script?
Asked Answered
P

4

15

TeamCity allows me to report back from my MsBuild script using the ##teamcity interaction. I can use this to tell TeamCity that the build has FAILED, or indeed SUCCEEDED, however I would like to tell it to CANCEL the build instead. Does anyone know of a way to do this?

I can use this to inform TeamCity of failure...

<Message Text="##teamcity[buildStatus status='FAILURE']" Condition="Something==SomeCondition" />

I would love to do this...

<Message Text="##teamcity[buildStatus status='CANCEL']" Condition="Something==SomeCondition" />

I've tried out the TeamCity Service Tasks but nothing thus far.

EDIT:

So it seems this feature is not available, although a workaround http request can be used to cancel a build. There is also a feature request for Cancelling a build the TC website.

Pontius answered 8/11, 2010 at 16:11 Comment(1)
See my answer for TeamCity 2019.1Vacation
V
9

According to JetBrains issue tracker and release page, since TeamCity 2019.1 EAP 1 builds can be stopped with service message as in:

##teamcity[buildStop comment='canceling comment' readdToQueue='true']
Vacation answered 8/5, 2019 at 5:39 Comment(0)
P
2

You can use the undocumented http request which has changed since it was originally posted. You now need "operationKind=1". I used a powershell runner like so:

$buildId = %teamcity.build.id%
$uri = "http://teamcity/ajax.html?guest=1&comment=Cancelling+build+for+some+reason&submit=Stop&buildId=$buildId&kill&operationKind=1"
$response = Invoke-WebRequest -UseBasicParsing -Uri $uri

Another SO post can tell you how to make an http request from MSBuild

The "guest=1" means I'm using the guest account, which at minimum needs the "Stop build / remove from queue" for the project you're going to cancel.

Phone answered 26/9, 2014 at 17:2 Comment(0)
B
2

Since Teamcity 8.1 (Source) it is possible to Cancel the Build via REST API.

Taken from the 9.x Documentation, cancelling a currently running build

curl -v -u user:password --request POST "http://teamcity:8111/app/rest/builds/<buildLocator>" --data "<buildCancelRequest comment='' readdIntoQueue='false' />" --header "Content-Type: application/xml"
Beedon answered 28/9, 2016 at 12:44 Comment(0)
O
1

Can you not just use the Error task, this should cause the execution of the build to stop.

Orsay answered 8/11, 2010 at 16:16 Comment(1)
Yeah I currently use this to stop the build but we want the behaviour within TC to be the same as hitting the Cancel button.Pontius

© 2022 - 2024 — McMap. All rights reserved.