lib/slackistrano/tasks/slack.rake in slackistrano-1.0.0 vs lib/slackistrano/tasks/slack.rake in slackistrano-1.1.0
- old
+ new
@@ -1,7 +1,8 @@
namespace :slack do
namespace :deploy do
+
def make_attachments(stage, options={})
attachments = options.merge({
title: fetch(:"slack_title_#{stage}"),
pretext: fetch(:"slack_pretext_#{stage}"),
text: fetch(:"slack_msg_#{stage}"),
@@ -10,108 +11,120 @@
mrkdwn_in: [:text, :pretext]
}).reject{|k, v| v.nil? }
[attachments]
end
+ def make_payload(stage)
+ payload = {
+ username: fetch(:slack_username),
+ icon_url: fetch(:slack_icon_url),
+ icon_emoji: fetch(:slack_icon_emoji),
+ }
+
+
+ payload[:attachments] = case stage
+ when :updated
+ make_attachments(stage, color: 'good')
+ when :reverted
+ make_attachments(stage, color: '#4CBDEC')
+ when :failed
+ make_attachments(stage, color: 'danger')
+ else
+ make_attachments(stage)
+ end
+
+ payload
+ end
+
+ def post_message(stage)
+ team = fetch(:slack_team)
+ token = fetch(:slack_token)
+ webhook = fetch(:slack_webhook)
+ via_slackbot = fetch(:slack_via_slackbot)
+ payload = make_payload(stage)
+
+ channels = fetch(:slack_channel)
+ stage_channel = "slack_channel_#{stage.to_s}".to_sym
+ if fetch(stage_channel)
+ channels = fetch(stage_channel)
+ end
+ channels = Array(channels)
+ if via_slackbot == false && channels.empty?
+ channels = [nil] # default webhook channel
+ end
+
+ channels.each do |channel|
+ payload[:channel] = channel
+
+ # This is a nasty hack, but until Capistrano provides an official way to determine if
+ # --dry-run was passed this is the only option.
+ # See https://github.com/capistrano/capistrano/issues/1462
+ if Capistrano::Configuration.env.send(:config)[:sshkit_backend] == SSHKit::Backend::Printer
+ info("[slackistrano] Slackistrano Dry Run:")
+ info("[slackistrano] Team: #{team}")
+ info("[slackistrano] Webhook: #{webhook}")
+ info("[slackistrano] Via Slackbot: #{via_slackbot}")
+ info("[slackistrano] Payload: #{payload.to_json}")
+
+ # Post to the channel.
+ else
+ http_response = Slackistrano.post(team: team,
+ token: token,
+ webhook: webhook,
+ via_slackbot: via_slackbot,
+ payload: payload)
+ if http_response.code !~ /^2/
+ error("[slackistrano] Slack API Failure!")
+ error("[slackistrano] URI: #{http_response.uri}")
+ error("[slackistrano] Code: #{http_response.code}")
+ error("[slackistrano] Message: #{http_response.message}")
+ error("[slackistrano] Body: #{http_response.body}") if http_response.message != http_response.body
+ end
+ end
+ end
+
+ end
+
+ ######################################################################
+
task :updating do
set(:slack_deploy_or_rollback, 'deploy')
if fetch(:slack_run_updating)
run_locally do
- Slackistrano.post(
- team: fetch(:slack_team),
- token: fetch(:slack_token),
- webhook: fetch(:slack_webhook),
- via_slackbot: fetch(:slack_via_slackbot),
- payload: {
- channel: fetch(:slack_channel_updating) || fetch(:slack_channel),
- username: fetch(:slack_username),
- icon_url: fetch(:slack_icon_url),
- icon_emoji: fetch(:slack_icon_emoji),
- attachments: make_attachments(:updating)
- }
- )
+ post_message(:updating)
end
end
end
task :reverting do
set(:slack_deploy_or_rollback, 'rollback')
if fetch(:slack_run_reverting)
run_locally do
- Slackistrano.post(
- team: fetch(:slack_team),
- token: fetch(:slack_token),
- webhook: fetch(:slack_webhook),
- via_slackbot: fetch(:slack_via_slackbot),
- payload: {
- channel: fetch(:slack_channel_reverting) || fetch(:slack_channel),
- username: fetch(:slack_username),
- icon_url: fetch(:slack_icon_url),
- icon_emoji: fetch(:slack_icon_emoji),
- attachments: make_attachments(:reverting)
- }
- )
+ post_message(:reverting)
end
end
end
-
task :updated do
if fetch(:slack_run_updated)
run_locally do
- Slackistrano.post(
- team: fetch(:slack_team),
- token: fetch(:slack_token),
- webhook: fetch(:slack_webhook),
- via_slackbot: fetch(:slack_via_slackbot),
- payload: {
- channel: fetch(:slack_channel_updated) || fetch(:slack_channel),
- username: fetch(:slack_username),
- icon_url: fetch(:slack_icon_url),
- icon_emoji: fetch(:slack_icon_emoji),
- attachments: make_attachments(:updated, color: 'good')
- }
- )
+ post_message(:updated)
end
end
end
task :reverted do
if fetch(:slack_run_reverted)
run_locally do
- Slackistrano.post(
- team: fetch(:slack_team),
- token: fetch(:slack_token),
- webhook: fetch(:slack_webhook),
- via_slackbot: fetch(:slack_via_slackbot),
- payload: {
- channel: fetch(:slack_channel_reverted) || fetch(:slack_channel),
- username: fetch(:slack_username),
- icon_url: fetch(:slack_icon_url),
- icon_emoji: fetch(:slack_icon_emoji),
- attachments: make_attachments(:reverted, color: '#4CBDEC')
- }
- )
+ post_message(:reverted)
end
end
end
task :failed do
if fetch(:slack_run_failed)
run_locally do
- Slackistrano.post(
- team: fetch(:slack_team),
- token: fetch(:slack_token),
- webhook: fetch(:slack_webhook),
- via_slackbot: fetch(:slack_via_slackbot),
- payload: {
- channel: fetch(:slack_channel_failed) || fetch(:slack_channel),
- username: fetch(:slack_username),
- icon_url: fetch(:slack_icon_url),
- icon_emoji: fetch(:slack_icon_emoji),
- attachments: make_attachments(:failed, color: 'danger')
- }
- )
+ post_message(:failed)
end
end
end
end