namespace :slack do desc 'Notify Slack of a deployment via the incoming webhooks integration - ' \ ':slack_subdomain and :slack_token must be set' task :notify_started do run_locally do set :time_started, Time.now.to_i if fetch(:slack_notify_events).include? :started info 'Notifying Slack of deploy starting' execute :curl, '-X POST', '--data-urlencode', Slackify::Payload.build(self, :starting), fetch(:slack_url) end end end before 'deploy:starting', 'slack:notify_started' desc 'Notify Slack of a deployment via the incoming webhooks integration - ' \ ':slack_subdomain and :slack_token must be set' task :notify_finished do run_locally do set :time_finished, Time.now.to_i if fetch(:slack_notify_events).include? :finished info 'Notifying Slack of deploy finished' execute :curl, '-X POST', '--data-urlencode', Slackify::Payload.build(self, :success), fetch(:slack_url) end end end after 'deploy:finished', 'slack:notify_finished' desc 'Notify Slack of a deployment failure via the incoming webhooks ' \ 'integration - :slack_subdomain and :slack_token must be set' task :notify_failed do run_locally do set :time_finished, Time.now.to_i if fetch(:slack_notify_events).include? :failed info 'Notifying Slack of deploy failed' execute :curl, '-X POST', '--data-urlencode', Slackify::Payload.build(self, :failed), fetch(:slack_url) end end end after 'deploy:failed', 'slack:notify_failed' end namespace :load do task :defaults do set :slack_channel, '#general' set :slack_username, 'Capistrano' set :slack_emoji, ':ghost:' set :slack_parse, 'default' set :slack_user, -> { local_user.strip } set :slack_hosts, -> { release_roles(:all).map(&:hostname).join("\n") } set :slack_url, -> { fail ':slack_url is not set' } set :slack_text, lambda { time_elapsed = Integer(fetch(:time_finished) - fetch(:time_started)) "#{fetch(:application)} deploy by #{fetch(:slack_user)}: " \ "successful in #{time_elapsed} seconds." } set :slack_deploy_starting_text, -> { "#{fetch(:application)} deploy by #{fetch(:slack_user)}: " \ "starting." } set :slack_deploy_failed_text, -> { "#{fetch(:application)} deploy by #{fetch(:slack_user)}: " \ "failed." } set :slack_deploy_starting_color, 'warning' set :slack_deploy_finished_color, 'good' set :slack_deploy_failed_color, 'danger' set :slack_notify_events, [:started, :finished, :failed] end end