Capistrano slack integration for deployment

In this tutorial, I will show you how you can do Capistrano slack integration for deployment so it will notify for every deployment. You will get notified after every deployment will finish. So let’s start.

Read Also: Capistrano rollback to specific release

You can use this https://api.slack.com/incoming-webhooks to setup incoming webhook notification. Click on incoming webhook notification to get started.

Then on the next page, you can select on which page you want to send the notification. See image below:

You might also like: Show current release version using Capistrano

After selecting the channel or user now its time to generate the token for incoming messages.

Integration with Capistrano

Now we already have webhook URL. Now add slack-notifier gem in your Gemfile. 

vim Gemfile

and add the following:

gem "slack-notifier"

Github link to slack-notifier: https://github.com/stevenosloan/slack-notifier

Create a new rake task

vim slack.rake

and paste  the following and make changes with your credentials

require 'slack-notifier'
require File.expand_path('../../../../config/environment', __FILE__)

namespace :slack do
desc "Notify via slack"
task :notify do
notifier = Slack::Notifier.new "https://hooks.slack.com/services/xxxxxx/xxxxxx/xxxxx
user = run_locally { %x{whoami}.strip }
message = "#{user} deployed to #{fetch(:rails_env)}"
end
end

Test

Now you can use cap -T command and you will see new tasks will be there and its called slack:notify.

Run the following command and you will see an alert in your slack channel.

cap staging slack:notify

 

Leave a Reply

Your email address will not be published.


*