Co-Op Capistrano Deployment Script
Yesterday at Harvest we launched a little API-accessible robot for our team communication app, Co-op. There are many uses for this API hook, from deploy notices to code commits to fun daily team affirmations. You are only limited by your imagination!
Our first use of the Cobot was to get deployment messages right in our Co-op workstream, including git commit messages. This is what we see today:
And our Capistrano deployment script (hat tip to John Nunemaker for the idea):
namespace :co_op do
desc "Update Co-op with most recent deployment messages."
task :update do
require 'net/http'
require 'base64'
require 'cgi'
require 'json'
app = "Harvest"
key = "withheld_for_your_protection"
headers = {
"Accept" => "application/json",
"Content-Type" => "application/json; charset=utf-8",
"User-Agent" => "Co-op Deployment"
}
begin
connection = Net::HTTP.new("coopapp.com", 80)
connection.open_timeout = 10
logs = (`git log #{previous_revision}..#{current_revision}`).
scan(/\n\s\s\s.+\n(?:\n|$)?/).
map{|l| l.strip}.
reject {|t| t.match(/^Merge branch
logs_size = logs.size
logs = logs[0,6]
logs << '...' if logs_size > 6
msg = "- #{logs.join('<br />- ')}"
who_am_i = `whoami`.chomp.capitalize
connection.post('/groups/5/notes',
{:status => "#{who_am_i} deployed #{app}: " +
"<br />#{msg}",
:key => key}.to_json,
headers)
puts 'Deploy notice sent to Co-op'
rescue Timeout::Error => e
puts "Timeout after 10s: Seems like Co-op missed your update."
puts "Use \"cap co_op:update\" to update Co-op " +
"status later w/o deploying"
end
end
end
after "deploy", "co_op:update"