After writing about “Running your TeamCity builds from PowerShell for any Git branch” I’d like to talk about another integration which is using Github Hubot so that the build could be started directly form a chat room.

So using the same idea, which is to extend our development environment, we implemented a way to start TeamCity builds directly from our HipChat room. You could do the same from Campfire for sure.

The main idea of this is to have our daily tools right at our disposal where we spend a good part of our days, chat rooms.

First we had to install Github Hubot and we have chosen to use Heroku which offer free hosting for one of their web dyno. They even offer what they call Hubot Factory which is a way to allow people to easily and quickly deploy new instances of Hubot to Heroku. In fact we went with a manual installation described on this page “Hubot for HipChat on Heroku”.

The idea is still the same we need to run TeamCity builds by “Accessing the Server by HTTP”.

This time we want to have Hubot calling the TeamCity server so we need to add a new CoffeeScript to the scripts of Hubot which will launch the HTTP request needed.

Our script is heavily inspired from the TeamCity.coffee script, and here is a first version:

TeamCity.coffeeTeamCity.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Starts a build on TeamCity.
#
# You need to set the following variables:
# HUBOT_TEAMCITY_USERNAME = <user name="">
# HUBOT_TEAMCITY_PASSWORD =
# HUBOT_TEAMCITY_HOSTNAME = <host port="" :="">
#
# start build buildId -- Starts TeamCiyt build using buildId
module.exports = (robot) ->
robot.respond /start build (.*)/i, (msg) ->
username = process.env.HUBOT_TEAMCITY_USERNAME
password = process.env.HUBOT_TEAMCITY_PASSWORD
hostname = process.env.HUBOT_TEAMCITY_HOSTNAME
buildName = msg.match[1]

msg.http("http://#{hostname}/httpAuth/action.html?add2Queue=#{buildName}")
.headers(Authorization: "Basic #{new Buffer("#{username}:#{password}").toString("base64")}",
Accept: "application/json")
.get() (err, res, body) ->
if err
msg.send "Team city says: #{err}"
return</host></user>

By the way I started to learn CoffeeScript so this is more hacking then something really productive.

And here is the result in our HipChat room asking hubot to start a build with the command:

hubot start build bt21

TeamCity, GitHub Hubot and HipChat

I will talk in another post about the last part which shows the status of the build in the chat room.