This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
forked from dmytro/capistrano-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify_slack.rb
91 lines (74 loc) · 2.4 KB
/
notify_slack.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
require 'uri'
require 'net/http'
require 'json'
set :user_name, `whoami`.chomp
def _send_message(message, channel)
payload = {
:text => message,
:channel => channel,
:username => 'Capiche Kun',
:icon_url => '',
:icon_emoji => ':capiche:',
}
uri = URI.parse(fetch(:webhook_url))
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
response
end
def _slack_channel
(fetch(:stage) == "localhost") ? "dev_test" : "#dev-deploy"
end
def _target_servers
servers = find_servers()
servers.map { |s| "#{s.host} (#{s.options[:hostname]})" }.join(", ")
end
namespace :slack do
namespace :deploy do
task :start do
msg = "<!channel>\n Hey guys, I just started deployment by order of my commander.\n"
msg << "\`\`\`\n"
if fetch(:only_infra, false)
msg << "Application : infrastructure}\n"
elsif fetch(:with_infra, false)
msg << "Application : #{fetch(:application)}, infrastructure\n"
else
msg << "Application : #{fetch(:application)}\n"
end
msg << "Branch/Tag : #{fetch(:branch)}\n"
msg << "Environment : #{fetch(:stage)}\n"
msg << "Target hosts : #{_target_servers}\n"
msg << "My commander : #{fetch(:user_name)}\n"
msg << "\`\`\`\n"
logger.info msg
_send_message(msg, _slack_channel)
end
task :finish do
msg = "[\`#{fetch(:application)}\`] Deployment complete, sir! :thumbsup:\n"
logger.info msg
_send_message(msg, _slack_channel)
end
end
namespace :rollback do
task :start do
msg = "[\`#{fetch(:application)}\`] Yes, my master. Rollback has started.\nCurrent Revision is \`#{fetch(:latest_revision)}\`"
logger.info msg
_send_message(msg, _slack_channel)
end
task :finish do
msg = "[\`#{fetch(:application)}\`] Rollback complete, sir! :ok_woman:\nCurrent revision is \`#{fetch(:current_revision)}\`"
logger.info msg
_send_message(msg, _slack_channel)
end
end
end
before 'deploy', 'slack:deploy:start'
after 'deploy:cleanup', 'slack:deploy:finish'
before 'deploy:rollback', 'slack:rollback:start'
after 'deploy:rollback:cleanup', 'slack:rollback:finish'