Capistrano – Email on Deployment

It’s a good practice to send a notification to certain people when a new version is deployed. This process can easily be automated in Capistrano. Add the following file to the ‘lib’ directory with the filename ‘cap_mailer.rb’:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
require 'rubygems'
require 'actionmailer'
 
ActionMailer::Base.smtp_settings = {:address => "localhost", :port => 25, :domain => 'example.com'}
 
class CapMailer < ActionMailer::Base
  def deploy_notification(options)    
    message = "Deployment\n\n"
    options.each_pair do |k,v|
      message << "  #{k}: #{v}\n"
    end
 
    recipients "notify-deployment@example.com"
    from       'noreply@example.com'
    subject    "Deployment to #{options[:rails_env]}"
    body       message
  end
end
 
module CapistranoMailer
  def send(options)
    CapMailer.deliver_deploy_notification(options) 
  end
end
 
Capistrano.plugin :mailer, CapistranoMailer

In the ‘deploy.rb’ file, add the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
require 'lib/cap_mailer'
require 'etc'
 
namespace :deploy do
  desc "Sends an email upon successful deployment"
  task :notify do
    mailer.send(
      :rails_env => rails_env,
      :deploy_to => deploy_to,
      :branch => branch,
      :application => application,
      :repository => repository,
      :deployment_time => Time.new.to_s,
      :deployer_username => Etc.getlogin)
  end
end
 
after "deploy", "deploy:notify"
after "deploy:migrations", "deploy:notify"
This entry was posted in Technology and tagged , , . Bookmark the permalink.

One Response to Capistrano – Email on Deployment

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">