Skip to content

Instantly share code, notes, and snippets.

@nosolopau
Created January 8, 2014 11:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save nosolopau/8315119 to your computer and use it in GitHub Desktop.
Save nosolopau/8315119 to your computer and use it in GitHub Desktop.
Send email notifications after deploy (with Capistrano and Rails 4). Capistrano variables reference: http://theadmin.org/articles/capistrano-variables/
# Add the following lines at the end of config/deploy.rb
Dir["config/deploy/extras/*.rb"].each { |file| load file }
set :notify_emails, ["mail@example.com"]
after "deploy", "deploy:notify"
# config/deploy/extras/notifier.rb
require 'action_mailer'
ActionMailer::Base.delivery_method = :sendmail # You can also use :smtp...
ActionMailer::Base.raise_delivery_errors = true
class Notifier < ActionMailer::Base
default from: '"notifier" <notifier@example.com>'
def deploy_notification(cap_vars)
application_name = cap_vars.application.split('_')[0]
mail(:to => cap_vars.notify_emails, :subject => "Application #{application_name} has been deployed in #{cap_vars.stage}") do |format|
format.text do
render :text => "Hi,\n\nThe application #{application_name} has been deployed in the #{cap_vars.stage} platform.\n\n#{cap_vars.release_notes}"
end
end
end
end
namespace :deploy do
desc "Email notifier"
task :notify, :roles => :app, :except => { :no_release => true } do
git_commits_range = "#{previous_revision.strip}..#{current_revision.strip}"
git_log = `git log --pretty=oneline --abbrev-commit #{git_commits_range}`
set :release_notes, git_log.blank? ? "No changes since last deploy." : "Last changes:\n" + git_log
Notifier.deploy_notification(self).deliver
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment