LoginSignup
16
17

More than 5 years have passed since last update.

capistranoデプロイ時に、新しくリリースされるgitのコミットログをHipChatに通知

Posted at

環境

  • Rails 3.2.18
  • capistrano 3.2.1
  • capistrano-pending 0.0.1
  • capistrano-rails 1.1.1
  • hipchat 1.2.0

capistrano-pending

capistrano-pendingというgemを利用した。

Gemfile
group :development do
  gem 'capistrano-pending', require: false
end

これを利用することで、capistrano3でもcap deploy:pendingコマンドが提供される。
デプロイ先のREVISIONファイルとローカルでのコミットを比較して、コミットの差分ログを表示してくれる。

実行すると、こんな感じのが表示される。

commit 94dc3ba967072e48baa39cd5754b838444a9e2ce
Merge: 13d432a df6ca9d
Author: Bob <hoge@example.com>
Date:   Mon Jun 23 09:55:19 2014 +0900

    Merge pull request #437 from Hoge/Fuga

    Add hoge

HipChatに通知

capistranoの設定を編集。
上記のcap deploy:pendingで得られる情報を適当に抜き出して、HipChatに通知する。
以下のように設定。(雑なコードですが)

lib/capistrano/hipchat.cap
namespace :hipchat do
  token = "<HipChat Token>"
  room_id = "<通知するRoomのID>"
  client = HipChat::Client.new(token, api_version: "v2")[room_id]

  def deploy_user
    `git config user.name`.strip || ENV['USER']
  end

  def release_commits
    commits_str = `cap #{fetch(:rails_env)} deploy:pending`
    commits = commits_str.split("\n\n")
    return if commits.blank?
    messages = []
    commits.each_with_index{|ci, index| messages << ci.strip if index.odd? }
    commiters = []
    commit_hashes = []
    commits.each_with_index do |info, index|
      next if index.odd?
      info_ary = info.split("\n")
      hash_elem = info_ary[0]
      commiter_elem = info_ary.select{|elem| elem if elem =~ /Author:/ }.first
      match_index = (commiter_elem =~ /</)
      commiters << commiter_elem[8..(match_index - 2)]
      commit_hashes << hash_elem[7..-1]
    end

    release_info = "<br><br>These commits are released at #{fetch(:rails_env)}!!<br>"
    (commits.length / 2).times do |n|
      release_info <<
        "- <a href='https://github.com/Hoge/#{fetch(:application)}/commit/#{commit_hashes[n]}'>" \
        "#{messages[n]}</a> " \
        "by #{commiters[n]}<br>"
    end
    release_info
  end

  namespace :update do
    desc 'Set releasing commit data'
    task :set_release_commit do
      run_locally do
        set :release_commits, release_commits
      end
    end

    desc "Notify when finishing deploy"
    task :finish do
      run_locally do
        client.send(
          "Capistrano",
          "#{deploy_user} finished deploying #{fetch(:application)}/#{fetch(:branch)} to #{fetch(:rails_env)}." \
          "#{fetch(:release_commits)}",
          { notify: true, color: 'green' }
        )
      end
    end
  end
end

before 'deploy:updating', 'hipchat:update:set_release_commit'
after 'deploy:finishing', 'hipchat:update:finish'

これで、デプロイ終了時、リリースされたコミットがGithubのリンク付きで表示される。

Bob finished deploying Hoge/master to production.

These commits are released at production!!
- Add hoge by Bob
- (以下略)

みたいな感じです。

16
17
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
17