Code Monkey home page Code Monkey logo

capistrano-slackify's Introduction

Capistrano Slackify Build Status Code Climate Gem Version

Publish deploy notifications to Slack - for Capistrano v3.

Installation

Add this line to your application's Gemfile:

gem 'capistrano-slackify', require: false

And then execute:

$ bundle

Or install it yourself as:

$ gem install capistrano-slackify

Usage

Require the gem in your Capfile:

require 'capistrano/slackify'

And then set the required variables in config/deploy.rb:

set :slack_url, 'https://hooks.slack.com/services/your/webhook/url'

Ensure that you have enabled the incoming webhooks integration - copy/paste the webhook url provided in the setup instructions.

The task will run automatically on deploy. Alternatively, you can notify of a deploy starting manually by using:

bundle exec cap production slack:notify_started

Or to notify of a finished deploy:

bundle exec cap production slack:notify_finished

By default, this will publish something along the lines of:

Revision 64a3c1de of my_app deployed to production by seenmyfate in 333 seconds.

If a deploy has failed, the following message will be published by default:

production deploy of my_app with revision/branch 64a3c1de failed

As with the other tasks, it is also possible to notify failures manually:

bundle exec cap production slack:notify_failed

Customisation

Any of the defaults can be over-ridden in config/deploy.rb:

set :slack_channel, ['#devops', '#other-channel']
set :slack_username, 'Deploybot'
set :slack_emoji, ':trollface:'
set :slack_user, ENV['GIT_AUTHOR_NAME']
set :slack_fields, ['status', 'stage', 'branch', 'revision', 'hosts']
set :slack_mrkdwn_in, ['pretext', 'text', 'fields']
set :slack_hosts, -> { release_roles(:all).map(&:hostname).join("\n") }
set :slack_text, -> {
  elapsed = Integer(fetch(:time_finished) - fetch(:time_started))
  "Revision #{fetch(:current_revision, fetch(:branch))} of " \
  "#{fetch(:application)} deployed to #{fetch(:stage)} by #{fetch(:slack_user)} " \
  "in #{elapsed} seconds."
}
set :slack_deploy_starting_text, -> {
  "#{fetch(:stage)} deploy starting with revision/branch #{fetch(:current_revision, fetch(:branch))} for #{fetch(:application)}"
}
set :slack_deploy_failed_text, -> {
  "#{fetch(:stage)} deploy of #{fetch(:application)} with revision/branch #{fetch(:current_revision, fetch(:branch))} failed"
}
set :slack_deploy_finished_color, 'good'
set :slack_deploy_failed_color, 'danger'
set :slack_notify_events, [:started, :finished, :failed]

You can setup custom fields by defining a mapping for how to display them in slack:

set :slack_fields, ['status', 'environment', 'docker_image']
# You could also add your custom mappings to the defaults with:
# set :slack_fields, fetch(:slack_fields).push('environment', 'docker_image')
set :slack_custom_field_mapping, -> {
  {
    'environment' => {
      title: 'Environment',
      value: -> {
        if fetch(:stage) == :production
          'production'
        else
          "staging-#{fetch(:staging_name)}"
        end
      },
      short: true,
    },
    'docker_image' => {
      title: 'Docker image',
      value: fetch(:docker_image),
      short: false,
    },
  }
}

To configure the way slack parses your message (see 'Parsing Modes' at https://api.slack.com/docs/formatting) use the :slack_parse setting:

set :slack_parse, 'none' # available options: 'none', 'full'

Copyright

Copyright (c) 2014 OnTheBeach Ltd. See LICENSE.txt for further details.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.