I am BARRY HESS > Blog

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:

Archive: Tuesday, Feb 16 at Team Iridesco @ Harvest Co-op

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"

      connection = Net::HTTP.new("coopapp.com", 80)
      connection.open_timeout = 10
      logs = (`git log #{previous_revision}..#{current_revision}`).
               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
                      {:status => "#{who_am_i} deployed #{app}: " +
                                  "<br />#{msg}",
                       :key => key}.to_json,
      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"

after  "deploy", "co_op:update"