Skip to content

Instantly share code, notes, and snippets.

@mindreframer
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindreframer/da9b48a9b3afedd63f7a to your computer and use it in GitHub Desktop.
Save mindreframer/da9b48a9b3afedd63f7a to your computer and use it in GitHub Desktop.
slack + capistrano
# lib/capistrano/tasks/slack.rake
# install slack-notifier gem
# include this file into your lib/capistrano/tasks/ folder
# add this line to the end of your Capfile:
# Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
# deploy with:
# MSG='fixing that payment bug...' cap staging deploy
class SlackNotifierWrapper
require 'slack-notifier'
def token
'XXXXXXXXXXX'
end
def team
"your-subdomain-on-slack"
end
def channel
'#deployments'
end
def app
"my-awesome-app"
end
def deployer
ENV['GIT_AUTHOR_NAME'] || `git config user.name`.chomp
end
def reason
raise "provice a reason with MSG=... !" unless ENV['MSG']
ENV['MSG']
end
def notify(msg)
notifier.ping(msg, icon_emoji: ":rocket:")
end
def notifier
@notifier ||= ::Slack::Notifier.new(team, token, channel: channel, username: 'capistrano')
end
end
namespace :slack do
def wrapper
@wrapper ||= SlackNotifierWrapper.new
end
def msg_prefix
deployer = wrapper.deployer
app = wrapper.app
branch = fetch(:branch, nil)
stage = fetch(:stage, 'production')
app_string = branch ? "#{app}'s #{branch}" : app
"#{deployer} / #{app_string} / #{stage}"
end
task :starting do
msg = "#{msg_prefix} - started - #{wrapper.reason}"
wrapper.notify(msg)
set(:start_time, Time.now)
end
task :finished do
begin
start_time = fetch(:start_time)
elapsed = Time.now.to_i - start_time.to_i
msg = "#{msg_prefix} - finished - #{elapsed} seconds."
wrapper.notify(msg)
end
end
before 'deploy', 'slack:starting'
after 'deploy', 'slack:finished'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment