Stephen Celis

Capistrano Growl notifications 6 Nov

It’s very easy to lose track of your deployment. A cap deploy and we’re on to other things. Time passes, our deploy failed, and we didn’t even know it! Let’s fix that.

Global cap configuration

Rake has Sake; Capistrano has…Capistrano.

One on the seemingly lesser-used features of Capistrano is its global configuration, ~/.caprc (Rake does have ~/.rake/*.rake files, as well). With bare-minimum effort, we can have tasks all over.

# ~/.caprc
Dir["#{ENV['HOME']}/.cap/*.rb"].each do |tasks|
  load tasks
end

From now on, when we save a Ruby script into our ~/.cap/ directory, its tasks will be available to Capistrano wherever cap is invoked.

Growling

For Growl to play nicely with Ruby, we’ll need Leopard’s standard Ruby installation or equivalent—with RubyGems, RubyCocoa: the works.

Wait…some of you don’t have Growl? Well if you don’t already have it, get it; install it; love it. If you’re just downloading it now, be sure to install growlnotify alongside it.

Beyond growlnotify, Meow is a great little gem by Aaron Patterson that’ll make growling in Ruby quite effortless:

% sudo gem install meow

With that, all that’s left is a Capfile for growling!

# ~/.cap/growl.rb
set :stage, nil unless defined? stage

namespace :growl do
  task :notify do
    growl_send(ENV["GROWL_MESSAGE"] || "wants your attention")
  end

  task :alert do
    growl_send(ENV["GROWL_MESSAGE"] || "needs your attention", 2)
  end
end

after "deploy" do
  ENV["GROWL_MESSAGE"] = "deployed #{application} #{stage}"
  find_and_execute_task "growl:notify"
end

after "rollback" do
  ENV["GROWL_MESSAGE"] = "rolled back #{application} #{stage}"
  find_and_execute_task "growl:alert"
end

def growl_send(message, p = 1)
  require 'meow'
  icon = OSX::NSWorkspace.sharedWorkspace.iconForFile(`which cap`.chomp) 
  Meow.notify("cap", "Capistrano", message, :priority => p, :icon => icon)
rescue LoadError
  `growlnotify -ncap -p#{priority} -m #{message.inspect} Capistrano`
end

Next time your deploy wraps up, you’ll know about it!

Capgrowl

Oh, and if it doesn’t go over so well (SIMULATION!):

Capfail

(For more information, see my dotfiles.)