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' Array(fetch(:slack_channel)).each {|channel| execute :curl, '-X POST', '-s', '--data-urlencode', Slackify::Payload.build(self, :starting, channel), 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' Array(fetch(:slack_channel)).each {|channel| execute :curl, '-X POST', '-s', '--data-urlencode', Slackify::Payload.build(self, :success, channel), 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' Array(fetch(:slack_channel)).each {|channel| execute :curl, '-X POST', '-s', '--data-urlencode', Slackify::Payload.build(self, :failed, channel), 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.to_s.strip } set :slack_fields, ['status', 'stage', 'branch', 'revision', 'hosts'] set :slack_custom_field_mapping, {} set :slack_mrkdwn_in, [] 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, 0) - fetch(:time_started, 0)) "#{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