Skip to content

Instantly share code, notes, and snippets.

@sorentwo
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save sorentwo/9072369 to your computer and use it in GitHub Desktop.

Select an option

Save sorentwo/9072369 to your computer and use it in GitHub Desktop.
Slack deploy notification task (adapted from capistrano-slack)
require 'json'
namespace :slack do
def post_to_slack(payload)
slack_subdomain = fetch(:slack_subdomain)
slack_token = fetch(:slack_token)
uri = URI.parse("https://#{slack_subdomain}.slack.com/services/hooks/incoming-webhook?token=#{slack_token}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data(payload: payload.merge(default_payload).to_json)
http.request(request)
end
def default_payload
{ channel: fetch(:slack_room), username: 'deploybot', icon_emoji: ':ghost:' }
end
desc 'Begin deploy notifications to Slack'
task :starting do
announced_deployer = fetch(:deployer)
announced_stage = fetch(:stage)
application = fetch(:slack_application)
post_to_slack(text: "#{announced_deployer} is deploying #{application} to #{announced_stage}")
set(:start_time, Time.now)
end
task :finished do
announced_deployer = fetch(:deployer)
application = fetch(:slack_application)
elapsed_time = Time.now.to_i - fetch(:start_time).to_i
post_to_slack(text: "#{announced_deployer} deployed #{application} successfully in #{elapsed_time} seconds.")
end
before 'deploy', 'slack:starting'
after 'deploy', 'slack:finished'
end
@dblandin
Copy link

Can we include the short git sha in the deploy notice?

@dblandin
Copy link

Maybe even a link to the commit on GitHub?

@dblandin
Copy link

Really awesome to see a custom slack integration at work!

@sorentwo
Copy link
Author

The SHA should be pretty easy, as we can just grab it with git. We could probably hack together a github link from the SHA if desired.

@dblandin
Copy link

Nice. Thinking of adapting this integration for the dscovr deploy process as well. Looks pretty straight-forward!

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