Post-Deploy Notifications on Engine Yard Cloud

Our latest post is from special guest and Engine Yard customer Matt Cowley. Matt is a freelance developer and the web manager at WMNF Community Radio. He can be found at http://www.madcowley.com. Matt originally published this post to his blog. Many thanks to Matt for pulling together the helpful info, and for allowing us to share it with you.

I’ve got a project hosted on Engine Yard’s cloud servers, with geographically-dispersed team. The developers are often working on separate feature sets, often deployed independently. As a quality-control and awareness measure we wanted to be notified when a deploy was done.

This was pretty easy to set up, and would work with capistrano deployments also.

First we set up a mailer action; I used an existing mailer class for this but you might want to create a separate class just for process-type notifications:

def deploy_notification(attribs)
  subject       "A build of my_project was deployed to #{attribs[:env_name]}"
  from          "MyProject CodeMonkey HQ"
  @from =       "[email protected]"
  recipients    "[email protected]"
  sent_on       Time.now
  body          :revision => attribs[:revision], :env_name => attribs[:env_name]
end

and the corresponding deploy_notification.text.plain.erb:

Heads up code monkeys! New code has been deployed to < %= @env_name %>

Details:

Environment: < %= @env_name %>
Revision: < %= @revision %>

I’ll probably make that a little fancier with a direct link to the commit on the githubs, and possibly pull in the commit message etc.

Next, we add a rake task to call the mailer; I put this in /lib/tasks/deploy.rake:

namespace :deploy do
  desc "notify developers of deployment"
  task :notify => :environment do
    attribs = {}
    ARGV.each do |arg|
      if arg.match(/TO=(.*)/)
        attribs[:env_name] = $1
      elsif arg.match(/REVISION=(.*)/)
        attribs[:revision] = $1
      end
    end
    SupportMailer.deliver_deploy_notification(attribs)
  end
end

Finally, for Engine Yard integration, in deploy/after_restart.rb:

# notify dev team of deploy
run "cd #{release_path} && rake deploy:notify TO=#{@configuration[:environment]} REVISION=#{@configuration[:revision]}"

That gets called by the engineyard chef scripts after the deploy finishes. For capistrano deployments, you could do the same in an “after deploy:restart” call.

That’s it. Hope it’s helpful.

Again, many thanks to Matt Cowley for this post originally published to his [MadCode blog](http://www.madcowley.com/madcode/2010/11/post-deploy-notifications-on-engineyard-cloud/) on November 24, 2010.