Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Commit

Permalink
Initial import.
Browse files Browse the repository at this point in the history
  • Loading branch information
parkr committed Feb 7, 2015
0 parents commit b083f5d
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
*.bundle
*.so
*.o
*.a
mkmf.log
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gemspec
22 changes: 22 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2015 Parker Moore

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Capistrano::SlackNotify

Capistrano 2 deploy notifier for Slack.

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'capistrano-slack-notify'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install capistrano-slack-notify

## Usage

Add the following to your `Capfile`:

```ruby
require 'capistrano-slack-notify'

set :slack_webhook_url, "https://hooks.slack.com/services/XXX/XXX/XXX"
```

That's it! It'll send 2 messages to `#general` as the `capistrano` user when you deploy.
You can optionally set some other parameters:

```ruby
set :slack_room, '#my_channel'
set :slack_username, 'my-company-bot'
set :slack_emoji, ':ghost:'
set :deployer, ENV['USER'].capitalize
```

## Contributing

1. Fork it ( https://github.com/parkr/capistrano-slack-notify/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "bundler/gem_tasks"
Binary file added Screen Shot 2015-02-06 at 5.52.10 PM.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions capistrano-slack-notify.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# coding: utf-8

Gem::Specification.new do |spec|
spec.name = "capistrano-slack-notify"
spec.version = "1.0.0"
spec.authors = ["Parker Moore"]
spec.email = ["[email protected]"]
spec.summary = %q{Minimalist Capistrano 2 notifier for Slack.}
spec.homepage = "https://github.com/parkr/capistrano-slack-notify"
spec.license = "MIT"

spec.files = `git ls-files -z`.split("\x0").grep(%r{^(bin|lib)/})
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_runtime_dependency "capistrano", "~> 2.10"

spec.add_development_dependency "bundler", "~> 1.7"
spec.add_development_dependency "rake", "~> 10.0"
end
1 change: 1 addition & 0 deletions lib/capistrano-slack-notify.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'capistrano/slack_notify'
79 changes: 79 additions & 0 deletions lib/capistrano/slack_notify.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
require 'capistrano'
require 'json'
require 'net/http'

module Capistrano
module SlackNotify
def call_slack_api(message)
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(message))
http.request(request)
rescue SocketError => e
puts "#{e.message} or slack may be down"
end

def slack_defaults
if fetch(:slack_deploy_defaults, true)
before 'deploy', 'slack:starting'
after 'deploy', 'slack:finished'
end
end

def payload(announcement)
{
'channel' => fetch(:slack_room, '#platform'),
'username' => fetch(:slack_username, 'capistrano'),
'text' => announcement,
'icon_emoji' => fetch(:slack_emoji, ':rocket:')
}.to_json
end

def slack_webhook_url
fetch(:slack_webhook_url)
end

def self.extended(configuration)
configuration.load do
slack_defaults

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
msg = if branch = fetch(:branch, nil)
"#{fetch(:deployer)} is deploying #{fetch(:application)}/#{branch} to #{fetch(:stage, 'production')}"
else
"#{fetch(:deployer)} is deploying #{fetch(:application)} to #{fetch(:stage, 'production')}"
end
call_slack_api(msg)
set(:start_time, Time.now)
end

desc "Notify Slack that the deploy has completed successfully."
task :finished do
msg = "#{fetch(:deployer)} deployed to #{fetch(:application)} successfully"
if start_time = fetch(:start_time, nil)
elapsed = Time.now.to_i - start_time.to_i
msg << " in #{elapsed} seconds."
else
msg << "."
end
call_slack_api(msg)
end
end # end namespace :slack
end
end # end self.extended

end
end

if Capistrano::Configuration.instance
Capistrano::Configuration.instance.extend(Capistrano::SlackNotify)
end

0 comments on commit b083f5d

Please sign in to comment.