Integrating TeamCity with Gerrit using PowerShell – Part 1


Right now, I’m in a project where we use Google’s gerrit for code review, and JetBrains Team City for CI. Both of these tools are good by themselves, but if they could combine their powers, we’d gain a lot in terms of reviewing capabilities, offloading the burden of verifying the build to Team City (compiling, running unit tests, anlysing code etc.) , while developers focus on actually reviewing.

In this three-part series, I’ll show how this can be set up, and using some new features from Powershell 3 to simplify it.

For this setup to work, we’re going to need the following:

  • Powershell >= 3
  • SSH (Comes with git, if you don’t have it already)
  • gerrit
  • TeamCity >= 7.1

This is actually a pretty simple setup, but we’re going to do this in three distinct steps. In this first step, we’ll look at how to get the changes from Gerrit to TeamCity

As you may already know, gerrit has two git trees for every project. One if of course the standard git tree that members of the project share and pull and push to, and the other is  a tree that contains all the commits that have been or will be reviewed. Once a commit passes to the review, it is then copied to the actual tree.

The first thing we need to do is to enable team city to build a certain gerrit change, directly pulled from the review tree instead of the ordinary tree. Luckily all gerrit changes are exposed to the user as special branches, under /refs/changes/, so this is a pretty simple task.

  • Create a copy of your git VCS root. I named my copy “gerrit changes”
  • Create a copy of the build configuration you want to run against gerrit changes. Probably the one that compiles and runs unit tests. I named my copy ”gerrit verify build and unit tests”
  • In the verify build step, create a new build parameter called ”gerrit.ref”, and set the VCS root of that project to ”gerrit”.
  • In the VCS root, change the “refs” property from whatever it was before to “%gerrit.ref%”.

To verify that this works, go to any gerrit change under your project and copy the ref from that change. Then run a custom build of your new gerrit project with that gerrit ref as the “gerrit.ref” parameter

Gerrit refs

Build a gerrit ref in teamcity

Running this, it should check out the change directly from gerrit (which can be verified both because team city will label the branch as refs/changes but also because you should see the new commit in the change log.

This is a good start. Now we can with a bit of manual work defer the building and testing to Team City, while we focus on reviewing. Coming up, we’ll look at how to automatically trigger changes.

This Post Has One Comment

  1. Pål K. Tønder

    Hi! We are at the moment struggling with integrating Gerrit and TeamCity on the windows platform so this is exactly what we are looking for. When will we see part 2 and 3 (Need this asap :-))

Leave a Reply