久しぶりにCircle CIを試したら、起動が妙に速くなっていた(前からこんなに速かったっけ?)のでいくつかのプロジェクトで使ってみる事にした。

Slackのインテグレーションがlimitに達していたので、Hubotで通知させるのを書いた。

circleci-webhook.coffee

# Description:
# Circle CI Webhook
#
# Author:
# @shokai
config =
room: "#news"
debug = require('debug')('hubot:circleci-webhook')
module.exports = (robot) ->
robot.router.post '/circleci-webhook', (req, res) ->
debug 'received /circleci-webhook'
room = req.query.room or config.room
room = "##{room}" unless /^#/.test room
status = req.body.payload?.status
subject = req.body.payload?.subject
build_url = req.body.payload?.build_url
branch = req.body.payload?.branch
reponame = req.body.payload?.reponame
username = req.body.payload?.username
unless room? and status? and subject? and build_url? and branch? and reponame? and username?
return res.status(400).end 'bad request'
res.end 'ok'
header = if status is 'failed' then ':no_good:' else ':ok_hand:'
text =
[
"#{header} [#{status.toUpperCase()}] #{subject} [#{username}/#{reponame}##{branch}]"
build_url
].join '\n'
debug text
robot.send {room: room}, text

設定

こんな感じでwebhook設定するとJSONが来るので、HubotがSlackに通知してくれる。

circle.yml
machine:
  node:
    version: 0.12
deployment:
  staging:
    branch: master
    heroku:
      appname: (herokuのアプリ名)
notify:
  webhooks:
    - url: https://自分のhubot.com/circleci-webhook?room=(chat部屋名)

動作

Hubot自体のビルド通知も、自分自身にやらせてる。
CircleCIでテスト→Herokuにデプロイ→Hubot起動→CircleCIからビルド結果がWebhookで届く→HubotがSlackに通知 という順に動いている。