Question

Stash 2.1 comes with a new REST API that allows you to tell Stash about builds related to specific changesets. How do I let Stash know about my builds in TeamCity?

Was it helpful?

Solution 3

In your build configurations, insert this Powershell script as the first build step:

curl -v -H "Content-Type: application/json" -X POST -d '{ \"state\": \"INPROGRESS\", \"key\": \"%teamcity.build.id%\", \"name\": \"#%build.number%: %system.teamcity.buildConfName%; %system.teamcity.projectName%\", \"url\": \"http://TEAMCITY-HOSTNAME/viewLog.html?buildId=%teamcity.build.id%\", \"description\": \"Revision: %build.vcs.number%; Triggered by: %build.triggeredBy%\" }' http://USERNAME:PASSWORD@STASH-HOSTNAME/rest/build-status/1.0/commits/%build.vcs.number%

This will let Stash know that a build for a certain changeset has started.

As your last build step, insert this Powershell script and select the option to execute it even though your build fails:

$xml = [xml](curl --request GET http://USERNAME:PASSWORD@TEAMCITY-HOSTNAME/httpAuth/app/rest/builds/%teamcity.build.id%)
Microsoft.PowerShell.Utility\Select-Xml $xml -XPath "/build" | %% { $status = $_.Node.status }
switch ($status) {
 "SUCCESS" { $stashStatus = "SUCCESSFUL"; }
 default { $stashStatus = "FAILED"; }
}
$do = @'
curl -v -H "Content-Type: application/json" -X POST -d '{ \"state\": \"$stashStatus\", \"key\": \"%teamcity.build.id%\", \"name\": \"#%build.number%: %system.teamcity.buildConfName%; %system.teamcity.projectName%\", \"url\": \"http://TEAMCITY-HOSTNAME/viewLog.html?buildId=%teamcity.build.id%\", \"description\": \"Revision: %build.vcs.number%; Triggered by: %build.triggeredBy%\" }' http://USERNAME:PASSWORD@STASH-HOSTNAME/rest/build-status/1.0/commits/%build.vcs.number%
'@
$do = $do -Replace '\$stashStatus', "$stashStatus"
Invoke-Expression $do

This will let Stash know that a build for a certain changeset has either succeeded or failed.

OTHER TIPS

You can use this TeamCity plugin which posts to the REST API with build statuses.

Note: I am the developer

Edit: Jetbrains also have a plugin that does the same thing, see here:

http://confluence.jetbrains.com/display/TW/Commit+Status+Publisher

JetBrains now has an official plugin called "Commit Status Publisher" that can send build status to Atlassian Stash or the Gerrit Code Review tool.

Source code is on GitHub.

Note: After installing the plugin, add a build feature, called "Commit status publisher" to your TeamCity build.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top