Capistranoでデプロイした時にGithubのどのPullRequestをリリースしたかをSlackに通知する

f:id:kimromi:20160709032809j:plain

上のような感じで、Capistranoでデプロイした時にどのPullRequestがリリースされたかをSlackに通知するGemを作りました。

github.com

Capistrano v3 のみ対応です。masterブランチにマージされた最新のPullRequestを取得して通知しますので、デプロイするときは一旦masterブランチに切り替えてgit pullしてからデプロイすると間違いないです。

@linyows さんが作ったcapistrano-github-releasescapistrano-slack_notificationに完全に依存しています。ありがとうございます!

セットアップ

Gemfile

gem 'capistrano-releases-notification'

Capfile

require 'capistrano/github/releases'        # 依存
require 'capistrano/slack_notification'     # 依存
require 'capistrano/releases/notification'

config/deploy.rb

# slack webhook
set :slack_endpoint, 'https://hooks.slack.com'
set :slack_path, '/services/xxxxxxxxx/yyyyyyyyy/zzzzzzzzzzzzzzzzzzzzzzz'

# slack通知設定
set :release_notify_channel, ['#general']   # 通知チャンネル名(複数指定可)
set :release_notify_mention, ['@kimromi']   # メンション(複数指定可)

# Github Enterpriseの場合
Octokit.configure do |c|
  c.api_endpoint = 'http://your.enterprise.domain/api/v3'
  c.web_endpoint = 'http://your.enterprise.domain/'
end

config/deploy/production.rb

# デプロイ終了後に通知する設定
after 'deploy:finishing', 'release:notify'

必要なstageで追加してください。

使いみち

メッセージやタイトルも編集できますので、我がムームードメインではカスタマーサービスチームにメンションを飛ばして何がリリースされたかを通知して共有するようにしています。下の例ではPullRequestの内容まで取得しています。

f:id:kimromi:20160709032819j:plain

set :release_notify_mention, ['@cs']
set :release_notify_title, 'ムームードメイン'
set :release_notify_message, -> {
  "リリースしました。 #{fetch(:release_notify_mention).join(' ')}"
}
set :release_notify_attachment, -> {
  pull_request = Octokit.pull(fetch(:github_repo), fetch(:pull_request_id))
  [
    pull_request.title,
    pull_request.html_url,
    '------------',
    pull_request.body[0, 200]
  ].join("\n")
}

お試しあれ〜