Integrating TeamCity with using WebHooks

Posted on Updated on

Update: Since version 1.1 of tcWebHooks, this  is now much simpler. See A better way of integrating TeamCity with using WebHooks

Here is a quick example of how to integrate with TeamCity using the tcWebHooks plugin.

Messages posted to from TeamCity using the tcWebHooks plugin.
Messages posted to from TeamCity using the tcWebHooks plugin.

The first three messages were posted using the first example XML configuration below. The last two are more elaborate and utilise the second XML snippet.

Step by step

  1. Create a new integration in Slack by going to the “Integrations” page.
  2. Scroll to near the bottom and click on the “Incoming WebHooks” button.
  3. Choose a channel to post into and click “Add incoming webhook”. A new integration should be created.
  4. Find the section entitled “Your Unique Webhook URL” copy the URL underneath it including the Token at the end.
  5. Switch to your teamcity web page and create a new WebHook.
  6. Paste in the webhook URL from step 4 into the webhook config.
  7. Choose Name Value Pairs payload format and select your trigger events. I suggest only selecting Successful and Failed. Otherwise you will fill your Slack channel with too many events. You may only want to choose when a build changes state to further reduce noise.
  8. After you’ve saved your webhook config, you need to add the Slack specific payload options by editing the WebHooks configuration file on the TeamCity server. In TeamCity 8 it will be located at: $HOME/.BuildServer/config/projects/<your_project_name>/pluginData/plugin-settings.xml
  9. Find your newly created WebHook and add the following block of XML before the </webhook> tag.
    <param name="payload" value="{&quot;text&quot;: &quot;${buildName} &lt;${buildStatusUrl}|build #${buildNumber}&gt; triggered by ${triggeredBy} has a status of ${buildResult}&quot;}"/>

The above XML snippet shows a simple message. Below is a more sophisticated example showing the use of the attachment JSON field to show a coloured bar and heading text on the message.

   <param name="payload" value="{  &quot;text&quot;: &quot;All your build failures are belong to us&quot;, &quot;attachments&quot;: [ { &quot;fallback&quot;: &quot;${buildName} &lt;${buildStatusUrl}|build #${buildNumber}&gt; triggered by ${triggeredBy} has a status of ${buildResult}&quot;, &quot;text&quot;: &quot;${buildName} &lt;${buildStatusUrl}|build #${buildNumber}&gt; triggered by ${triggeredBy} has a status of ${buildResult}&quot;, &quot;color&quot;: &quot;danger&quot; } ] }"/>

A more complete example

This shows two full webhook XML configurations so you can see the parameter tag in context. If you look closely, you’ll see the first one only applies to build failures, and the second one only applies to build success messages. That is how you can display a different colour and message for different build results.

<?xml version="1.0" encoding="UTF-8"?>
  <webhooks enabled="true">
     <webhook url="https://<your_slack_url><your_token_here>" enabled="true" format="nvpairs">
        <state type="buildSuccessful" enabled="false" />
        <state type="buildInterrupted" enabled="false" />
        <state type="responsibilityChanged" enabled="false" />
        <state type="buildStarted" enabled="false" />
        <state type="buildFailed" enabled="true" />
        <state type="beforeBuildFinish" enabled="false" />
        <state type="buildFixed" enabled="false" />
        <state type="buildFinished" enabled="true" />
        <state type="buildBroken" enabled="false" />
      <build-types enabled-for-all="true" enabled-for-subprojects="true" />
        <param name="payload" value="{  &quot;text&quot;: &quot;All your build failures are belong to us&quot;, &quot;attachments&quot;: [ { &quot;fallback&quot;: &quot;${buildName} &lt;${buildStatusUrl}|build #${buildNumber}&gt; triggered by ${triggeredBy} has a status of ${buildResult}&quot;, &quot;text&quot;: &quot;${buildName} &lt;${buildStatusUrl}|build #${buildNumber}&gt; triggered by ${triggeredBy} has a status of ${buildResult}&quot;, &quot;color&quot;: &quot;danger&quot; } ] }"/>
    <webhook url="https://<your_slack_url><your_token_here>" enabled="true" format="nvpairs">
        <state type="buildSuccessful" enabled="true" />
        <state type="buildInterrupted" enabled="false" />
        <state type="responsibilityChanged" enabled="false" />
        <state type="buildStarted" enabled="false" />
        <state type="buildFailed" enabled="false" />
        <state type="beforeBuildFinish" enabled="false" />
        <state type="buildFixed" enabled="false" />
        <state type="buildFinished" enabled="true" />
        <state type="buildBroken" enabled="false" />
      <build-types enabled-for-all="true" enabled-for-subprojects="true" />
        <param name="payload" value="{  &quot;text&quot;: &quot;All your build successes should to be celebrated&quot;, &quot;attachments&quot;: [ { &quot;fallback&quot;: &quot;${buildName} &lt;${buildStatusUrl}|build #${buildNumber}&gt; triggered by ${triggeredBy} has a status of ${buildResult}&quot;, &quot;text&quot;: &quot;${buildName} &lt;${buildStatusUrl}|build #${buildNumber}&gt; triggered by ${triggeredBy} has a status of ${buildResult}&quot;, &quot;color&quot;: &quot;good&quot; } ] }"/>

There is a full list of the variables available.


new tcWebHooks release. Proper support for TeamCity nested projects

Posted on Updated on

The alpha release from last week is now available as the main release.

This release includes the following new features.

  1. Proper support for TeamCity’s nested project structure. You can now choose if a webhook is inherited by child projects.
  2. ExtraParameters can now be templated in a similar way to the htmlStatusMessage field. This has come in handy for FlowDock integration.

Nested Project support

Webhooks can be inherited by subprojects
Webhooks can be inherited by subprojects

Previous versions of the tcWebHooks plugin did not support nested projects at all. A webhook was configured for a project and only builds in that project would fire them. This version adds support for webhooks to be inherited from parent (and higher) projects.

To enable the old behaviour, I also added control of that feature to the UI as “sub-project builds”. By default webhooks are inherited, but that inheritance can be disabled by deselecting the “All Sub-Project Builds” check box.

Extra Parameter templates

Prior to the latest release, additional parameters were static text only. This release adds the ability to template them in the same manner as the buildStatusHtml message Custom Templates detailed in a previous post.

Here is an example of their use for integration with FlowDock.

 <webhook url="" enabled="true" format="nvpairs">
     <state type="buildFixed" enabled="false" />
     <state type="buildStarted" enabled="false" />
     <state type="buildBroken" enabled="false" />
     <state type="buildSuccessful" enabled="true" />
     <state type="buildFinished" enabled="true" />
     <state type="responsibilityChanged" enabled="false" />
     <state type="beforeBuildFinish" enabled="false" />
     <state type="buildFailed" enabled="true" />
     <state type="buildInterrupted" enabled="false" />
   <build-types enabled-for-all="false" enabled-for-subprojects="true">
     <build-type id="bt3" />
     <param name="content" value="${message}" />
     <param name="external_user_name" value="teamcity" />
     <param name="tags" value="${projectExternalId},${buildExternalTypeId},${buildResult},teamcity" />

These parameters will appear as part of the POST payload and have the ${variables} resolved as per the details in the previous post.

Download and feedback

As usual, please download and post any issues you find on the bugs page.

Custom Templates and branches in tcWebHooks

Posted on Updated on

New release available

There is a new release of the TeamCity WebHooks plugin (tcWebHooks –

  1. Adds branch details to the top level of the WebHook Payload Content object (fixes missing branch info in the Name/Value Pairs format).
  2. Fixes an incorrect link on the WebHooks tab of a build.

Download it from SourceForge

Feature Branches and tcWebHooks

I’ve spent the last few evenings working through various issues with the branch support I added a few releases ago. Because I don’t use a VCS that TeamCity supports for branch building, I have never had the opportunity to thoroughly test the feature.

With the help of Jeff, we worked through a number of issues with the Name/Value Pairs (nvpairs) format and my handling of the Branch interface that was added in TeamCity 7.0

The main problem was that the Branch object was being added to the WebHookPayloadContent, but as a child object. Therefore, structured payload formats like JSON and XML were serialising out the contents of the child object correctly, but the nvpairs format was not. It appears that most users are using the nvpairs format. Especially those POSTing to the HipChat webservice endpoint.

As a result of these changes, I have added the following items to the payload object and they are serialised out for all payload formats at the top level.

  • branchDisplayName – A friendly name like “master” or “feature01”.
  • branchName – A name used internally by TeamCity. In my testing, it shows as “<default>” for the “master” and “feature01” for the “feature01” branch.
  • branchIsDefault – A Boolean (note the uppercase B), that can be null, true or false depending on whether the build has feature branches enabled (null if not), is the master (true) or a branch (false).

These names are gleaned straight from the “Branch” interface in the TeamCity OpenAPI. I note with interest that the actual TeamCity implementation has different names (myBranchName, myDisplayName), so the JSON payload actually looks like this.

  "branchName": "<default>",
  "branchDisplayName": "master",
  "branchIsDefault": true,
  "branch": {
    "@class": "jetbrains.buildServer.serverSide.impl.BranchImpl",
    "myBranchName": "<default>",
    "myDisplayName": "master"

The upshot is that the WebHookPayloadContent bean is what the custom templates use for building up the htmlBuildStatus message. That means that when configuring a custom message, you can only expect to resolve variables from this bean (using the getters). If value is not set for this instance variable, you will get “null” back. If the getter does not exist, you will get “UNDEFINED”.

A gotcha with the Branch object supplied by TeamCity

The Branch object and the new payload variables are completely dependant on TeamCity passing a useful Branch object to the sRunningBuild.
Please be aware that you will only get a sensible set of branch variables if all the following are true:

  1. You are running TeamCity version 7.1 or higher.
  2. The VCS that the build is built from has support for feature branches provided by TeamCity (currently in TC8 this is only GIT and Mecurial).
  3. You have configured feature branch support in your build so that TeamCity is aware that you have more than one branch that could potentially be used for versioning your build. For more information, please see:

If any of these are not correct, you will get null values for the new branch variables supplied by tcWebhooks.

A list of the object variables in the WebHookPayloadContent bean

As of tcWebHooks verion, you should expect the following to be available to the buildStatusHtml template engine.

Variable Name Example Value
projectId GitTestBuilds
message Build Git Test Builds :: Git Test Build has finished. This is build number 22, has a status of “success” and was triggered by Net Wolf
buildStatus Tests passed: 2
buildId 76
buildStateDescription finished
extraParameters null
buildInternalTypeId bt9
branchName feature01
comment null
class class webhook.teamcity.payload.content.WebHookPayloadContent
projectExternalId GitTestBuilds
agentHostname localhost
text Git Test Builds :: Git Test Build has finished. Status: success
buildExternalTypeId GitTestBuilds_GitTestBuild
buildResultPrevious success
projectName Git Test Builds
buildTypeId GitTestBuilds_GitTestBuild
triggeredBy Net Wolf
buildResult success
buildRunners [Maven]
agentOs Linux, version 3.2.0-51-generic
agentName Default Agent
buildName Git Test Build
buildFullName Git Test Builds :: Git Test Build
branchDisplayName feature01
buildResultDelta unchanged
projectInternalId project5
notifyType buildFinished
buildNumber 22
branchIsDefault false

tcWebHooks 0.9 out of alpha

Posted on

The release has been promoted to be the default download

It supports TC8 and TC7 (and probably earlier versions) and is ready for anybody who wants to use it. Please post any feedback below or on the bugs page.

tcWebHooks 0.9 update

Posted on

For those trying out the new 0.9 branch of tcWebHooks, here is a new version that supports editing webhooks applicable to the build you are viewing.

Also a couple of bug fixes.

  • Adding a new webhook was broken when there were no existing webhooks (adding the first webhook for a project).
  • Some Javascript debugging using Firebugs console, which might have errored on browsers that don’t have Firebug installed.
  • Removed some extra debugging from the javascript code.

Please try it out and let me know if you find any more bugs.

Updated tcWebHooks plugin with build specific webhooks

Posted on Updated on

A frequently requested feature for tcWebHooks is to be able to trigger webhook requests from specific builds in a TeamCity project rather than all builds in a project.


  • You can now select which build(s) a webhook triggers on.
  • Completely rewritten UI – might be bugs.
  • Tested on TC7 and TC8.
  • It’s alpha – Download and feedback please.

The whole story

I’m excited to announce an alpha release of the 0.9 branch which supports choosing which builds a webhook triggers on. I’d really like feedback on the usability and any bugs you find.

Editing Webhooks UI completely re-written

There are a few changes in the core webhook triggering logic which are fairly well tested. However, there has been a major re-write of the javascript and backend MVC controllers to support this extra hierarchy of information being sent back to the browser after an update. This includes moving to JSON as the edit response payload and re-writing nearly all the javascript in the edit webhooks page. This is primarily where any bugs could have crept in. I have tested on Firefox and Chrome, but don’t own a non-linux PC, so have no opportunity to test on browsers from proprietary operating systems.

The changes in the UI are visible on the following pages:

The updated WebHooks edit page

The tcWebHooks editing page now shows how many builds are configured for each webhook.

Screenshot of Edit webhooks page.
On the WebHooks editing page, each webhook shows how many builds in this project will trigger.

Updated WebHooks edit dialog

When editing a WebHook, you can choose which builds to run it on by clicking a new tab inside the Edit Webhooks popup dialog. Clicking the build count will open the dialog directly on the Builds tab. Clicking anywhere else on a webhook opens the dialog on the Webhook Config tab.

Screenshot of new Edit Webhooks dialog
The Edit Webhook dialog now has a Builds tab.

Updated WebHooks list on the Project and Build WebHook tabs

The WebHooks tab on Project pages and Build pages list webhooks per build.

I have broken down the webhooks tabs on the builds and projects pages to show webhooks relevant to all builds in a project as well as webhooks tied to specific builds.

Screenshot of Project tab.
The webhooks tab on Project and Build pages show webhooks grouped by build.

NOTE: this is one aspect that has not been completed. Editing the webhooks for a project takes you to the project webhooks page (shown above) and that page is all working. However, clicking “Edit Build Webhooks” is not currently working. I’ll keep working on that, but wanted to get this alpha out for testing while I worked on that page.

Tested on TC 7.1 and TC8

I have run the plugin on both TeamCity 7.1.5 and 8.0.0. They appear to be working fine despite the huge changes in the TeamCity OpenAPI between those versions. TeamCity introduced the ability to change ProjectId and BuildId values. This means there are now getInternal and getExternal (or similar) methods on the SProject and SBuildType interfaces. Compliing against the old API breaks stuff at runtime in TC8 and compiling against the TC8 API throws no such method errors everywhere.

To mitigate this, I’ve written a wrapper around the methods I use which falls back to the old methods if the new ones are not available. I’m hoping I’ve caught them all and from my testing of the plugin it works on both TC7.1 and TC8. Please post a comment below or on the bugs page if you experience any issues.

Please try it and post any feedback

You can download the release from sourceforge. Please post any feedback in the comments of this post or on the tcWebHooks bugs page.

Making tcWebHooks configurable via TeamCity’s REST API : please discuss

Posted on Updated on

The TeamCity REST API looks to be quite popular (I’ve not knowingly used it). I’ve had a request to add REST API support to tcWebHooks. I’ve not written a public webservices API before. Only really web APIs for services that I control both ends of.

Note: This is not a commitment to build it, more a place to put my thoughts on whether it’s worthwhile or not and how it might work.

I see the four basic requirements.

  • List
  • Create
  • Update
  • Delete
  • Is there anything else needed? Perhaps test?

I think there are three ways it could be accomplished.

  1. Write it myself and probably get it wrong.
  2. Write it myself with a lot input from the users. Who is willing to commit to helping me spec it and test it?
  3. Write a Java API and let someone else write the webservice parts.

The stuff I’ve done before was sending and receiving JSON, and only used POST and GET. Do I need to worry about the whole strict PUT, DELETE etc verbs?

What should the request payload look like? XML? JSON? Name Value Pairs?
What should the response payload look like?

Any good examples? I presume basing it on the existing TeamCity REST API would be the path of least surprise for users. I’ve not used it so don’t have a lot of experience on how it works.