6

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?

Makoto
  • 104,088
  • 27
  • 192
  • 230

3 Answers3

13

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

Mendhak
  • 8,194
  • 5
  • 47
  • 64
8

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.

C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
  • 7
    For anyone else who installed this plugin and then spends 45 minutes googling how to use it, you have to add a build feature, called "Commit status publisher" – BigJoe714 Apr 09 '15 at 23:11
  • That's the problem you can hardly find any tutorials on how to use it. You install the plugin and then what?! Thanks @BigJoe714 for the clarification. – Daniel B Feb 15 '16 at 01:48
7

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.