Skip to content

Instantly share code, notes, and snippets.

@davidalger
Last active February 24, 2020 15:19
Show Gist options
  • Save davidalger/16f9583739141770930e473ff0ff877a to your computer and use it in GitHub Desktop.
Save davidalger/16f9583739141770930e473ff0ff877a to your computer and use it in GitHub Desktop.
Integrate capistrano/slackify with capistrano-magento2 deployments
# Include gem used for Slack deploy notifications
require 'capistrano/slackify'
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
set :slack_url, '<slack_webhook_url>'
set :slack_username, 'capistrano-magento2'
set :slack_channel, ['#channel-name']
set :slack_emoji, ':capistrano:' # Custom emoji of capistrano logo uploaded to Slack
set :slack_deploy_pending_format, '--pretty="format:%h %ad %aN %s" --date=short'
# Change the label on 'stage' to avoid confusion
set :slack_custom_field_mapping, -> {
{
'stage' => {
title: 'Environment',
value: fetch(:stage),
short: true
}
}
}
include Capistrano::Magento2::Pending
module Slackify
class PendingPayload < Payload
def payload(channel)
MultiJson.dump(
{
channel: channel,
username: fetch(:slack_username),
icon_emoji: fetch(:slack_emoji),
text: fetch(:slack_pending_changes_text).join("\n"),
}
)
end
end
end
namespace :slack do
task :notify_pending do
set :slack_pending_changes_text, '>>>'
on roles fetch(:magento_deploy_pending_role) do |host|
ensure_revision true do
# update local repository to ensure accuracy of report
run_locally do
execute :git, :fetch, :origin
end
# fetch current revision and revision to be deployed
from = from_rev
to = to_rev
# if there is nothing to deploy on this host, inform the user
if from == to
append :slack_pending_changes_text, "*==> #{host}* (no changes to deploy)"
else
run_locally do
append :slack_pending_changes_text, "*==> #{host}* (#{from} -> #{to})"
# capture log of commits between current revision and revision for deploy
output = capture :git, :log, "#{from}..#{to}", fetch(:slack_deploy_pending_format)
# if we get no results, flip refs to look at reverse log in case of rollback deployments
if output.to_s.strip.empty?
output = capture :git, :log, "#{to}..#{from}", fetch(:slack_deploy_pending_format)
if not output.to_s.strip.empty?
append :slack_pending_changes_text,
"*Warning: It appears you may be going backwards in time with this deployment!*"
end
end
# write pending changes log
append :slack_pending_changes_text, "```#{output}```"
end
end
end
end
# set :slack_pending_changes_text, pending_changes
run_locally do
info 'Notifying Slack of changelog pending deploy'
Array(fetch(:slack_channel)).each {|channel|
execute :curl, '-X POST', '-s', '--data-urlencode',
Slackify::PendingPayload.build(self, :pending, channel),
fetch(:slack_url)
}
end
end
after 'deploy:pending:log', 'slack:notify_pending'
end
@davidalger
Copy link
Author

Slack-capitrano-magento2-notification

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment