How to let Hubot send status messages to your Jabber conference after a Jenkins build was finished
Wouldn’t it be nice if Hubot would tell you if a jenkins build was successful or not? Here is how we did it by extending the jenkins script at https://github.com/github/hubot-scripts/blob/master/src/scripts/jenkins.coffee
Add a callback-method so jenkins can trigger an action
jenkinsCallback = (req, res, robot) ->
room = "room@your.jabber.server"
job = req.body.job
user = robot.userForId 'broadcast'
user.room = room
user.type = 'groupchat'
url = process.env.HUBOT_JENKINS_URL
getreq = robot.http("#{url}/job/#{job}/api/json")
if process.env.HUBOT_JENKINS_AUTH
auth = new Buffer(process.env.HUBOT_JENKINS_AUTH).toString('base64')
getreq.headers Authorization: "Basic #{auth}"
getreq.get() (err, res, body) ->
if err
robot.send user, "Jenkins callback failed: #{err}"
else
try
message = "Jenkins: #{job} "
content = JSON.parse(body)
message+= if content.color == "red" then "FAILED" else "PASSED"
robot.send user, message
catch error
robot.send user, error
res.writeHead 200, {'Content-Type': 'text/plain'}
res.end 'Ok'
Add a route to receive callbacks
module.exports = (robot) ->
robot.router.post "/your/path/for/jenkins/callbacks", (req, res) ->
jenkinsCallback(req, res, robot)
Make Jenkins send callbacks
We use the PostBuildScript-Plugin to trigger the callbacks:
curl -d job=$JOB_NAME http://example.com/your/path/for/jenkins/callbacks
This assumes that Hubot is listening on port 80 of example.com, so adjust it to your needs.
We are using jabber to communicate with our team members, and as this was just a quick hack, we only implemented sending xmpp messages. Feel free to write a comment how to use a general messaging approach, then this might be worth a github pull request.