Skip to content

Instantly share code, notes, and snippets.

@say8425
Last active September 3, 2018 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save say8425/bfd8cbebe241c7bec1c829092624f618 to your computer and use it in GitHub Desktop.
Save say8425/bfd8cbebe241c7bec1c829092624f618 to your computer and use it in GitHub Desktop.
Capistrano Slack
require 'slackistrano/capistrano'
require_relative 'lib/slack_message'
set :slackistrano, {
channel: '#your_channel',
webhook: 'https://hooks.slack.com/services/foo/bar/baz/qux',
klass: Slackistrano::SlackMessage,
}
gem 'slackistrano'
#lib/slack_message.rb
if defined?(Slackistrano::Messaging)
module Slackistrano
class SlackMessage < Messaging::Base
def username
# displaied user name in Slack
end
def icon_url
# disaplaied user icon in Slack
end
def payload_for_updating
{
attachments: [{
color: '#29B2EF',
title: 'Deploing is starting :rocket:',
fields: [{
title: 'Environment',
value: stage,
short: true
}, {
title: 'Branch',
value: branch,
short: true
}, {
title: 'Deployer',
value: deployer,
short: true
}],
fallback: super[:text]
}]
}
end
def payload_for_updated
{
attachments: [{
color: 'good',
title: 'Deploing is success :sparkles:',
fields: [{
title: 'Environment',
value: stage,
short: true
}, {
title: 'Branch',
value: branch,
short: true
}, {
title: 'Deployer',
value: deployer,
short: true
}, {
title: 'Time',
value: elapsed_time,
short: true
}],
fallback: super[:text]
}]
}
end
def payload_for_failed
{
attachments: [{
color: 'danger',
title: 'Deploing is fail :boom:',
fields: [{
title: 'Environment',
value: stage,
short: true
}, {
title: 'Branch',
value: branch,
short: true
}, {
title: 'Deployer',
value: deployer,
short: true
}, {
title: 'Time',
value: elapsed_time,
short: true
}],
fallback: super[:text]
}]
}
end
# Default reverted message. Alternatively simply do not redefine this
# method.
def payload_for_reverted
super
end
# Override the deployer helper to pull the best name available (git, password file, env vars).
# See https://github.com/phallstrom/slackistrano/blob/master/lib/slackistrano/messaging/helpers.rb
def deployer
name = `git config user.name`.strip
name = nil if name.empty?
name ||= Etc.getpwnam(ENV['USER']).gecos || ENV['USER'] || ENV['USERNAME']
name
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment