Sunday, 7 January 2018

VSTS and TeamCity – Wrapping up

Part 4 in a series on integrating VSTS with TeamCity

Wouldn't it be great if TeamCity and VSTS had full builtin support for each other? Well yes, yes it would! Maybe that will happen soon.

If I knew Java well, I could probably have a go at writing a TeamCity addin that encapsulates most of the what the pull request server does - but the idea of spending a few weeks getting up to speed with Java/TeamCity development doesn’t excite me that much.

TeamCity 2017.2 adds VSTS Git support to the Commit Status Publisher build feature. I haven’t been able to try this out yet (due to some other bugs in 2017.2 preventing me from upgrading), but it is possible this could remove or reduce the requirement for the build completion handler.

VCS post-commit hook

Now you've seen how to to use the APIs for TeamCity and VSTS, you might also want to implement another optimisation - adding a VCS post-commit hook. You add an additional service hook in VSTS that notifies TeamCity that there's a code change so that TeamCity knows it should grab the latest commit(s).
  1. In VSTS Project Settings, go to the Service Hooks tab
  2. Click '+' to add a new service hook
  3. Select Web Hooks
  4. In Trigger on this type of event, select Code pushed
  5. Optionally, review the Filters and just check the Repository (and branch) that should trigger the event.
  6. In the URL, enter something like,count:99999),property:(name:url,value:%2Fbuildname,matchType:contains),count:99999
    the locator can vary depending on your individual requirements
  7. Enter the username and password to authenticate with TeamCity
  8. Set Resource details to send, Messages to send and Detailed messages to send to None
  9. Click Test to confirm that everything works.

The nice thing about this is that rather than TeamCity blindly polling VSTS, VSTS is telling TeamCity when it has something of interest.

Wednesday, 3 January 2018

VSTS with TeamCity – Configuration

Part 3 in a series on integrating VSTS with TeamCity

Now that we've created the pull request server, we need to configure VSTS and TeamCity so that they can send event messages to it.


If you followed the steps in the sample tutorial, this will be familiar.
  1. Go to the Service Hooks tab for your project
  2. Click on the + icon
  3. Choose Web Hooks and click Next
  4. Select Pull request created.
    New Service Hooks Subscription dialog window screenshot
  5. If appropriate, select a specific repository and click Next
  6. In URL, enter the URL that VSTS will use to connect to the pull request server, including a query string that defines which TeamCity build should be queued.
    Eg. If the pull request server is hosted at and the TeamCity build type id is My_CI_Build then you’d use
  7. In Username and Password, enter the credentials that will be used to authenticate with TeamCity
  8. Leave Resource details to send as All.
  9. Set Messages to send and Detailed messages to send to None
  10. Click on Test to try it out.
  11. Click on Finish to save this service hook.
  12. Repeat these steps to create another service hook for Pull request updated, also setting the Change filter to Source branch updated.
With the service hooks in place, you can now go to the Branches page, and click on the … (more actions) icon and choose Branch policies.
Selecting branch policy from more actions menu

The Add status policy button should be enabled, and clicking on that you should be able to find the pull request server listed in the drop down.


To allow TeamCity to call the pull request server, you will need to install the Web Hooks plugin for TeamCity. With that in place, go to the build configuration page in TeamCity, and you’ll see a new WebHooks tab.
  1. Click on add build Webhooks, then Click to create new WebHook for this build and add a new web hook for the project
  2. In the URL, enter the URL that TeamCity will use to connect to the pull request server.
    Eg. If the pull request server is hosted at, you would use
  3. Set the payload format to Legacy webhook (JSON)
  4. Clear all the trigger events except On Completion Trigger when build successful and Trigger when build fails
  5. Click Save

In the VCS Root settings for the VSTS Git repository, set Branch Specification to

We don't need TeamCity to trigger the builds for the pull request branches as the pull request server will be queuing those builds, but we do still want TeamCity to trigger the master builds.

In the build configuration VCS Trigger, set the Branch filter to +:<default>

With all that configuration done, creating a new pull request in VSTS should now trigger a branch build in TeamCity. When the build completes, the status is posted back to VSTS, allowing the pull request to be completed by merging the changes into master.