module Capistrano::SlackNotify

Constants

HEX_COLORS

Public Class Methods

extended(configuration) click to toggle source
# File lib/capistrano/slack_notify.rb, line 126
def self.extended(configuration)
  configuration.load do
    # Add the default hooks by default.
    set :deployer do
      ENV['USER'] || ENV['GIT_AUTHOR_NAME'] || `git config user.name`.chomp
    end

    namespace :slack do
      desc "Notify Slack that the deploy has started."
      task :starting do

        on_rollback do
          post_to_channel(:red, "#{deployer} *failed* to deploy #{deploy_target} to #{destination}")
        end

        post_to_channel(:grey, "#{deployer} is deploying #{deploy_target} to #{destination}")
        set(:start_time, Time.now)
      end

      desc "Notify Slack that the rollback has completed."
      task :rolled_back do
        post_to_channel(:green, "#{deployer} has rolled back #{deploy_target}")
      end

      desc "Notify Slack that the deploy has completed successfully."
      task :finished do
        msg = "#{deployer} deployed #{deploy_target} to #{destination} *successfully*"

        if start_time = fetch(:start_time, nil)
          msg << " in #{Time.now.to_i - start_time.to_i} seconds."
        else
          msg << "."
        end

        post_to_channel(:green, msg)
      end

      desc "Notify Slack that the deploy failed."
      task :failed do
        post_to_channel(:red, "#{deployer} *failed* to deploy #{deploy_target} to #{destination}")
      end
    end # end namespace :slack
  end
end

Public Instance Methods

attachment_payload(color, announcement) click to toggle source
# File lib/capistrano/slack_notify.rb, line 44
def attachment_payload(color, announcement)
  {
    'channel'     => slack_channel,
    'username'    => slack_username,
    'icon_emoji'  => slack_emoji,
    'attachments' => [{
      'fallback'  => announcement,
      'text'      => announcement,
      'color'     => HEX_COLORS[color],
      'mrkdwn_in' => %w{text}
    }]
  }.to_json
end
branch() click to toggle source
# File lib/capistrano/slack_notify.rb, line 110
def branch
  @branch ||= fetch(:branch, '')
end
call_slack_api(payload) click to toggle source
# File lib/capistrano/slack_notify.rb, line 22
def call_slack_api(payload)
  uri = URI.parse(slack_webhook_url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  request = Net::HTTP::Post.new(uri.request_uri)
  request.set_form_data(:payload => payload)
  http.request(request)
rescue SocketError => e
   puts "#{e.message} or slack may be down"
end
deploy_target() click to toggle source
# File lib/capistrano/slack_notify.rb, line 122
def deploy_target
  slack_app_and_branch + (rev ? " (#{rev[0..5]})" : '')
end
deployer() click to toggle source
# File lib/capistrano/slack_notify.rb, line 82
def deployer
  fetch(:deployer)
end
destination() click to toggle source
# File lib/capistrano/slack_notify.rb, line 90
def destination
  fetch(:slack_destination, stage)
end
post_to_channel(color, message) click to toggle source
# File lib/capistrano/slack_notify.rb, line 14
def post_to_channel(color, message)
  if use_color?
    call_slack_api(attachment_payload(color, message))
  else
    call_slack_api(regular_payload(message))
  end
end
regular_payload(announcement) click to toggle source
# File lib/capistrano/slack_notify.rb, line 34
def regular_payload(announcement)
  {
    'channel'    => slack_channel,
    'username'   => slack_username,
    'text'       => announcement,
    'icon_emoji' => slack_emoji,
    'mrkdwn'     => true
  }.to_json
end
repository() click to toggle source
# File lib/capistrano/slack_notify.rb, line 94
def repository
  fetch(:repository, 'origin')
end
rev() click to toggle source
# File lib/capistrano/slack_notify.rb, line 102
def rev
  @rev ||= if @branch.nil?
             fetch(:revision)
           else
             revision_from_branch
           end
end
revision_from_branch() click to toggle source
# File lib/capistrano/slack_notify.rb, line 98
def revision_from_branch
  `git ls-remote #{repository} #{branch}`.split(' ').first
end
slack_app_and_branch() click to toggle source
# File lib/capistrano/slack_notify.rb, line 114
def slack_app_and_branch
  if branch.nil?
    slack_app_name
  else
    [slack_app_name, branch].join('/')
  end
end
slack_app_name() click to toggle source
# File lib/capistrano/slack_notify.rb, line 78
def slack_app_name
  fetch(:slack_app_name, fetch(:application))
end
slack_channel() click to toggle source
# File lib/capistrano/slack_notify.rb, line 66
def slack_channel
  fetch(:slack_room, '#platform')
end
slack_emoji() click to toggle source
# File lib/capistrano/slack_notify.rb, line 74
def slack_emoji
  fetch(:slack_emoji, ':rocket:')
end
slack_username() click to toggle source
# File lib/capistrano/slack_notify.rb, line 70
def slack_username
  fetch(:slack_username, 'capistrano')
end
slack_webhook_url() click to toggle source
# File lib/capistrano/slack_notify.rb, line 62
def slack_webhook_url
  fetch(:slack_webhook_url)
end
stage() click to toggle source
# File lib/capistrano/slack_notify.rb, line 86
def stage
  fetch(:stage, 'production')
end
use_color?() click to toggle source
# File lib/capistrano/slack_notify.rb, line 58
def use_color?
  fetch(:slack_color, true)
end