Skip to content

Instantly share code, notes, and snippets.

@johnthethird
Forked from rtekie/cap_notify.rb
Created May 4, 2011 20:02
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save johnthethird/955917 to your computer and use it in GitHub Desktop.
Save johnthethird/955917 to your computer and use it in GitHub Desktop.
Capistrano deployment email notifier for Rails 3
=begin
Capistrano deployment email notifier for Rails 3
Do you need to send email notifications after application deployments?
Christopher Sexton developed a Simple Capistrano email notifier for rails. You can find details at http://www.codeography.com/2010/03/24/simple-capistrano-email-notifier-for-rails.html.
Here is Rails 3 port of the notifier.
The notifier sends an email after application deployment has been completed.
How to use it?
1. Add this file to config/deploy folder.
2. Update the file with your google credentials and from email address.
3. Add the following content to config/deploy.rb.
require 'config/deploy/cap_notify.rb'
# add email addresses for people who should receive deployment notifications
set :notify_emails, ["EMAIL1@YOURDOMAIN.COM", "EMAIL2@YOURDOMAIN.COM"]
after :deploy, 'deploy:send_notification'
# Create task to send a notification
namespace :deploy do
desc "Send email notification"
task :send_notification do
Notifier.deploy_notification(self).deliver
end
end
4. Update deploy.rb with destination email addresses for the notifications.
5. To test run this command:
cap deploy:send_notification
=end
require "action_mailer"
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:tls => true,
:address => "smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:authentication => "plain",
:user_name => "YOUR USER NAME",
:password => "YOUR PASSWORD"
}
class Notifier < ActionMailer::Base
default :from => "YOUR FROM EMAIL"
def deploy_notification(cap_vars)
now = Time.now
msg = "Performed a deploy operation on #{now.strftime("%m/%d/%Y")} at #{now.strftime("%I:%M %p")} to #{cap_vars.host}"
mail(:to => cap_vars.notify_emails,
:subject => "Deployed #{cap_vars.application} to #{cap_vars.stage}") do |format|
format.text { render :text => msg}
format.html { render :text => "<p>" + msg + "<\p>"}
end
end
end
@pablasso
Copy link

Note to include the gem tlsmail so this works with any mail over ssl.

@vrybas
Copy link

vrybas commented Dec 14, 2012

I had to use load instead of require here because of

   rubygems/custom_require.rb:55:in `require': cannot load such file -- config/deploy/cap_notify.rb (LoadError)

@ivomarino
Copy link

I've fixed this a bit for Capistrano 3:

cap_notify.rb:

require 'action_mailer'

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
  :enable_starttls_auto => false,
  :tls => false,
  :ssl => true,
  :address => 'mail.foo.com',
  :port => 465,
  :domain => 'foo.com',
  :authentication => 'plain',
  :user_name => 'bar@foo.com',
  :password => 'password'
}

class Notifier < ActionMailer::Base
  default :from => 'deploy@ttss.ch'
  def deploy_notification(cap_vars)
    now = Time.now
    msg = "Performed a deployment of <b>#{fetch(:application)}</b>, on #{now.strftime("%m/%d/%Y")} at #{now.strftime("%I:%M %p")}, to <b>#{fetch(:stage)}</b>.<br />Latest commit is <b><a href=\"http://code.foo.com/r#{fetch(:cap_notify_callsign)}#{fetch(:cap_notify_latest_commit)}\">#{fetch(:cap_notify_latest_commit)}</a></b>."

    mail(:to => fetch(:cap_notify_emails),
      :subject => "[CAPISTRANO] Deployed #{fetch(:application)} to #{fetch(:stage)}") do |format|
      format.text { render :text => msg}
      format.html { render :text => "<p>" + msg + "<\p>"}
    end
  end
end

deploy.rb:

set :application, 'my-project'
set :repo_url, 'ssh://git@code.foo.com:2222/diffusion/PROJECT/project.git'
set :default_env, { path: "/usr/local/bin:$PATH" }

set :keep_releases, 2

set :nc_terminal, 'com.googlecode.iterm2'

set :cap_notify_emails, [ 'tech@foo.com' ]
set :cap_notify_from, 'deploy@foo.com'
set :cap_notify_callsign, 'PROJECT'
set :cap_notify_latest_commit, proc { `git rev-parse HEAD`.strip }

# Branch options
# Prompts for the branch name (defaults to current branch)
#ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }

# Sets branch to current one
#set :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }

# Hardcodes branch to always be master
# This could be overridden in a stage config file
set :branch, :master

set :deploy_to, -> { "/var/www/vhosts/#{fetch(:application)}" }

set :log_level, :info
# set :log_level, :debug

set :linked_files, %w{.env web/.htaccess web/app/plugins/w3tc-wp-loader.php web/app/advanced-cache.php}
set :linked_dirs, %w{web/app/uploads web/app/languages web/app/cache web/app/w3tc-config web/app/ewww}

namespace :deploy do

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      # Your restart mechanism here, for example:
      # execute :service, :nginx, :reload
    end
  end

  desc "Send email notification"
  task :send_notification do
    Notifier.deploy_notification(self).deliver_now
  end

end

after :deploy, 'deploy:send_notification'

@ElRochito
Copy link

Hi,

I've got this error and I don't understand what I can do to fix it

(Backtrace restricted to imported tasks)
cap aborted!
LoadError: cannot load such file -- action_mailer

Tasks: TOP => staging
(See full trace by running task with --trace)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment