Skip to content

Instantly share code, notes, and snippets.

@artifactsauce
Last active December 11, 2017 17:03
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artifactsauce/a75509598559aac2ad67 to your computer and use it in GitHub Desktop.
Save artifactsauce/a75509598559aac2ad67 to your computer and use it in GitHub Desktop.
Notification tasks for Slack with Capistrano.
namespace :notify do
namespace :update do
task :start do
_send_message("[\`#{fetch(:application)}\`] Deployment has started.")
end
task :finish do
_send_message("[\`#{fetch(:application)}\`] Deployment has finished. :ok_woman:\n> #{fetch(:commit_message)}")
end
end
namespace :rollback do
task :start do
_send_message("[\`#{fetch(:application)}\`] Rollback has started.\nCurrent Revision is \`#{fetch(:latest_revision)}\`")
end
task :finish do
_send_message("[\`#{fetch(:application)}\`] Rollback has finished. :ok_woman:\nCurrent revision is \`#{fetch(:current_revision)}\`")
end
end
end
before 'deploy:starting', 'notify:update:start'
after 'deploy:finishing', 'notify:update:finish'
before 'deploy:reverting', 'notify:rollback:start'
after 'deploy:finishing_rollback', 'notify:rollback:finish'
namespace :git do
task :get_commit_message do
repository_path = "#{fetch(:deploy_to)}/repo"
on roles(:app) do
within repository_path do
commit_message = capture :git, 'log', '-1', '--pretty=format:"%h - %an : %s" HEAD'
set :commit_message, commit_message.force_encoding("UTF-8")
end
end
end
end
before 'notify:update:finish', 'git:get_commit_message'
require 'uri'
require 'net/http'
require 'json'
def _send_message(message)
payload = {
:text => message,
:channel => '<CHANGE_THIS>',
:username => 'Deploy Agent',
:icon_emoji => ':shipit:',
}
teamname = '<CHANGE_THIS>'
token = '<CHANGE_THIS>'
uri = URI.parse("https://#{teamname}.slack.com/services/hooks/incoming-webhook?token=#{token}")
response = nil
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data({ :payload => JSON.generate( payload ) })
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.start do |h|
response = h.request(request)
end
return response
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment