circleci_deployment_notifier 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 35da5d43b5ea74ce0d122ab2626a4150de8b8a09
4
- data.tar.gz: 556b7b145b1ff5514e61e267f984897c488ab8d7
3
+ metadata.gz: d33310b4336ae0ac91b9649691dbeaf7070569fe
4
+ data.tar.gz: 4714fc08c4a00f01c33091d366975608687b27e9
5
5
  SHA512:
6
- metadata.gz: 6ac68e893bd18dcdbb25e117f0d8480a2bf3c50d63ffe2c3e29eddbf45abe941a5391d2f73c98fcb4e3b319396c2c092db44291d228d99ce5121d9781d95bd42
7
- data.tar.gz: 41e5dfc603b75eb9700ad5503aeb3ae9ea0e21f947befc4e947eedc953a8b2eed565e43725797e2debabbb8607f7409c1f40c3e47827627f382a55c9c58e302a
6
+ metadata.gz: 1818a8cdb6e29cd6d81a79fce46352bf208370e9a7619812599c27cf71bd4a36c089e42a508c9eb45916565cb6ef24a23c137cd6f0dd1e83d18e8fae5681715d
7
+ data.tar.gz: 50969eaeef8bc73658c2d212addec59381f256e71ff00a0b493d2b9f10c03e02f8b0b54cf52979d5fa524e39eb9e768895cb32759f73d835683347692f69cece
data/.rubocop.yml ADDED
@@ -0,0 +1,39 @@
1
+ #
2
+ # For more information see https://github.com/bbatsov/rubocop/blob/master/config/default.yml
3
+ #
4
+ # However, do not include entire blocks here just because you want to change them.
5
+ # Include only the specific lines that you're changing from the defaults.
6
+ #
7
+ AllCops:
8
+ TargetRubyVersion: 2.1
9
+
10
+ Style/IfUnlessModifier:
11
+ MaxLineLength: 100
12
+ Style/WhileUntilModifier:
13
+ MaxLineLength: 100
14
+ Metrics/LineLength:
15
+ Max: 100
16
+ Style/BracesAroundHashParameters:
17
+ Enabled: false
18
+ Style/ClassCheck:
19
+ Enabled: false
20
+ Style/DotPosition:
21
+ EnforcedStyle: leading
22
+ Style/IndentHash:
23
+ EnforcedStyle: consistent
24
+ Style/MultilineOperationIndentation:
25
+ EnforcedStyle: indented
26
+ Style/AlignParameters:
27
+ EnforcedStyle: with_fixed_indentation
28
+ Style/BlockDelimiters:
29
+ EnforcedStyle: semantic
30
+ Style/StringLiterals:
31
+ Enabled: false
32
+ Style/StringLiteralsInInterpolation:
33
+ Enabled: false
34
+ Style/SymbolArray:
35
+ Enabled: true
36
+ Style/TrailingCommaInLiteral:
37
+ EnforcedStyleForMultiline: comma
38
+ Style/TrailingCommaInArguments:
39
+ EnforcedStyleForMultiline: comma
data/.travis.yml CHANGED
@@ -1,5 +1,8 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.3.1
4
+ - 2.1.10
5
+ - 2.2.6
6
+ - 2.3.3
7
+ - 2.4.0
5
8
  before_install: gem install bundler -v 1.13.6
data/Gemfile CHANGED
@@ -2,4 +2,3 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in circleci_deployment_notifier.gemspec
4
4
  gemspec
5
- gem 'codacy-coverage', require: false
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
-
1
+ [![Build Status](https://travis-ci.org/RobinDaugherty/circleci_deployment_notifier.svg?branch=master)](https://travis-ci.org/RobinDaugherty/circleci_deployment_notifier)
2
2
  [![Codacy Badge](https://api.codacy.com/project/badge/Grade/b76abb83eb5449499cbe3740c147715e)](https://www.codacy.com/app/robindaugherty/circleci_deployment_notifier?utm_source=github.com&utm_medium=referral&utm_content=RobinDaugherty/circleci_deployment_notifier&utm_campaign=Badge_Grade)
3
3
  [![Codacy Coverage](https://api.codacy.com/project/badge/Coverage/b76abb83eb5449499cbe3740c147715e)](https://www.codacy.com/app/robindaugherty/circleci_deployment_notifier?utm_source=github.com&utm_medium=referral&utm_content=RobinDaugherty/circleci_deployment_notifier&utm_campaign=Badge_Coverage)
4
+ [![Dependency Status](https://gemnasium.com/badges/github.com/RobinDaugherty/circleci_deployment_notifier.svg)](https://gemnasium.com/github.com/RobinDaugherty/circleci_deployment_notifier)
4
5
 
5
6
  # CircleCI Deployment Notifier
6
7
 
@@ -16,24 +17,21 @@ You can also:
16
17
 
17
18
  - Deploy releases that are tagged using Github Releases.
18
19
 
19
- ## Installation
20
+ ## Usage
20
21
 
21
22
  Requires Ruby 2.0.0 or higher.
22
23
 
23
- ```sh
24
- gem install circleci_deployment_notifier
25
- ```
26
-
27
- ## Usage
28
-
29
- Add the following lines to your `circle.yml` in the deployments section:
24
+ Add the following commands to your `circle.yml` deployment section:
30
25
 
31
26
  ```sh
32
27
  gem install circleci_deployment_notifier
33
28
  circleci_deployment_notify_slack -a "Application Name" -u "https://hooks.slack.com/services/WEBHOOK"
34
29
  ```
35
30
 
36
- Here's an example deployment section from `circle.yml`:
31
+ It's best to use environment variables for sensitive information list Slack webhook URLs.
32
+
33
+ So here's an example `circle.yml` deployment section that
34
+ assumes you've set the environment variable `DEPLOYMENT_SLACK_WEBHOOK_URL`:
37
35
  ```yml
38
36
  deployment:
39
37
  staging:
@@ -41,16 +39,21 @@ deployment:
41
39
  commands:
42
40
  - # TODO: something to deploy the application to staging
43
41
  - gem install circleci_deployment_notifier
44
- - circleci_deployment_notify_slack -a "Application Staging" -u "https://hooks.slack.com/services/WEBHOOK"
42
+ - circleci_deployment_notify_slack -a "Application Staging" -u $DEPLOYMENT_SLACK_WEBHOOK_URL
45
43
  production:
46
44
  tag: /v[0-9]+\.[0-9]+\.[0-9]+/
47
45
  owner: MyOrganization
48
46
  commands:
49
47
  - # TODO: something to deploy the application to production
50
48
  - gem install circleci_deployment_notifier
51
- - circleci_deployment_notify_slack -a "Application Production" -u "https://hooks.slack.com/services/WEBHOOK"
49
+ - circleci_deployment_notify_slack -a "Application Production" -u $DEPLOYMENT_SLACK_WEBHOOK_URL
52
50
  ```
53
51
 
52
+ That `production` section is designed to build and deploy for tags like "v1.0.0" that are created in
53
+ the fork of the repo that belongs to MyOrganization.
54
+ (So tags created in forks do not cause a build or deployment.)
55
+ See [CircleCI docs](https://circleci.com/docs/configuration/#tags) for more info.
56
+
54
57
  ## Development
55
58
 
56
59
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -61,6 +64,15 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
61
64
 
62
65
  Bug reports and pull requests are welcome on GitHub at https://github.com/RobinDaugherty/circleci_deployment_notifier.
63
66
 
67
+ ## FAQ
68
+
69
+ ### Why is this project built on Travis when it's specifically for CircleCI?
70
+
71
+ CircleCI does not support builds against multiple versions of Ruby, so for a tool like this, Travis
72
+ is a better fit.
73
+ This gem is for use by applications, where it will be built on a single version of Ruby and deployed
74
+ to staging and/or production infrastructure owned by the team.
75
+
64
76
  ## License
65
77
 
66
78
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -21,9 +21,10 @@ Gem::Specification.new do |spec|
21
21
  spec.require_paths = ["lib"]
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.13"
24
- spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rake", "~> 12.0"
25
25
  spec.add_development_dependency "rspec", "~> 3.0"
26
26
  spec.add_development_dependency "webmock", "~> 2.3"
27
+ spec.add_development_dependency "codacy-coverage"
27
28
  spec.add_dependency "slack-notifier", "~> 2.0"
28
29
  spec.add_dependency "slop", "~> 4.4"
29
30
  end
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'circleci_deployment_notifier'
4
+ require 'slop'
5
+ require 'uri'
6
+
7
+ puts "CircleCI Deployment Notify New Relic"
8
+
9
+ opts = Slop.parse do |o|
10
+ o.string '-a', '--app-name', 'Name of the application that was deployed (e.g. "Asgard Production")'
11
+ o.string '-k', '--api-key', 'New Relic API Key'
12
+ o.string '-i', '--app-id', 'New Relic App ID'
13
+ o.on '--version', 'print the version' do
14
+ puts CircleciDeploymentNotifier::VERSION
15
+ exit
16
+ end
17
+ end
18
+
19
+ unless ENV['CIRCLECI']
20
+ STDERR.puts "ERROR: This is not a CircleCI build (missing environment variables)."
21
+ exit 1
22
+ end
23
+
24
+ app_name = opts[:app_name]
25
+ app_id = opts[:app_id]
26
+ api_key = opts[:api_key]
27
+
28
+ if app_name.nil?
29
+ STDERR.puts "ERROR: Missing App Name"
30
+ puts opts
31
+ exit 1
32
+ end
33
+
34
+ if api_key.nil?
35
+ STDERR.puts "ERROR: Missing New Relic API Key"
36
+ puts opts
37
+ exit 1
38
+ end
39
+
40
+ if app_id.nil?
41
+ STDERR.puts "ERROR: Missing New Relic App ID"
42
+ puts opts
43
+ exit 1
44
+ end
45
+
46
+ CircleciDeploymentNotifier::Build.new(app_name: app_name)
47
+ .send_to_new_relic(new_relic_api_key: api_key, new_relic_app_id: app_id)
@@ -20,22 +20,17 @@ unless ENV['CIRCLECI']
20
20
  exit 1
21
21
  end
22
22
 
23
- # unless ARGV.size == 2
24
- # STDERR.puts "Not enough arguments."
25
- # print_help
26
- # end
27
-
28
23
  app_name = opts[:app_name]
29
24
  webhook_url = opts[:webhook_url]
30
25
 
31
- if webhook_url.nil?
32
- STDERR.puts "ERROR: Missing Slack Webhook URL"
26
+ if app_name.nil?
27
+ STDERR.puts "ERROR: Missing App Name"
33
28
  puts opts
34
29
  exit 1
35
30
  end
36
31
 
37
- if app_name.nil?
38
- STDERR.puts "ERROR: Missing App Name"
32
+ if webhook_url.nil?
33
+ STDERR.puts "ERROR: Missing Slack Webhook URL"
39
34
  puts opts
40
35
  exit 1
41
36
  end
@@ -1,16 +1,37 @@
1
1
  require "circleci_deployment_notifier/build_info"
2
2
  require "circleci_deployment_notifier/slack"
3
+ require "circleci_deployment_notifier/new_relic"
3
4
 
4
5
  module CircleciDeploymentNotifier
6
+ ##
7
+ # The main object of this gem. Instantiated with the name of the application,
8
+ # can then be used to send a notification.
5
9
  class Build
10
+ ##
11
+ # @param app_name [String] Name of the application that was deployed.
6
12
  def initialize(app_name:)
7
13
  self.app_name = app_name
8
14
  end
9
15
 
16
+ ##
17
+ # Sends a notification to Slack using the Slack Webhook URL.
18
+ # @param webhook_url [String] Slack Webhook URL
10
19
  def send_to_slack(webhook_url:)
11
20
  Slack.new(webhook_url: webhook_url, app_name: app_name, build_info: build_info).send
12
21
  end
13
22
 
23
+ ##
24
+ # Sends a deployment notification to New Relic.
25
+ # @param new_relic_api_key [String] New Relic API Key
26
+ # @param new_relic_app_id [String] New Relic Application ID
27
+ def send_to_new_relic(new_relic_api_key:, new_relic_app_id:)
28
+ NewRelic.new(
29
+ new_relic_api_key: new_relic_api_key,
30
+ new_relic_app_id: new_relic_app_id,
31
+ build_info: build_info,
32
+ ).send
33
+ end
34
+
14
35
  private
15
36
 
16
37
  attr_accessor :app_name
@@ -1,4 +1,7 @@
1
1
  module CircleciDeploymentNotifier
2
+ ##
3
+ # Gets build information from the environment.
4
+ # Expects and currently only works with builds of Github repositories.
2
5
  class BuildInfo
3
6
  def commit_hash
4
7
  ENV['CIRCLE_SHA1']
@@ -0,0 +1,66 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'json'
4
+
5
+ module CircleciDeploymentNotifier
6
+ ##
7
+ # Sends deployment notification to New Relic.
8
+ # Builds the message using a BuildInfo object.
9
+ class NewRelic
10
+ ##
11
+ # @param new_relic_api_key [String] New Relic API Key
12
+ # @param new_relic_app_id [String] New Relic Application ID
13
+ # @param build_info [BuildInfo]
14
+ def initialize(new_relic_api_key:, new_relic_app_id:, build_info:)
15
+ self.new_relic_api_key = new_relic_api_key
16
+ self.new_relic_app_id = new_relic_app_id
17
+ self.build_info = build_info
18
+ end
19
+
20
+ ##
21
+ # Sends the deployment notification to New Relic.
22
+ # @return [Boolean] Whether the request was successful.
23
+ def send
24
+ http_response.code == 201
25
+ end
26
+
27
+ private
28
+
29
+ attr_accessor :new_relic_api_key
30
+ attr_accessor :new_relic_app_id
31
+ attr_accessor :build_info
32
+
33
+ def uri
34
+ URI("https://api.newrelic.com/v2/applications/#{new_relic_app_id}/deployments.json")
35
+ end
36
+
37
+ def http_connection
38
+ Net::HTTP.new(uri.host, uri.port).tap do |http|
39
+ http.use_ssl = true
40
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
41
+ end
42
+ end
43
+
44
+ def http_request
45
+ Net::HTTP::Post.new(uri).tap do |req|
46
+ req.add_field "X-Api-Key", new_relic_api_key
47
+ req.add_field "Content-Type", "application/json"
48
+ req.body = JSON.dump body_data
49
+ end
50
+ end
51
+
52
+ def body_data
53
+ {
54
+ deployment: {
55
+ revision: build_info.tag_name || build_info.branch_name,
56
+ user: build_info.builder_username,
57
+ description: build_info.tag_release_notes_url || build_info.commit_browse_url,
58
+ },
59
+ }
60
+ end
61
+
62
+ def http_response
63
+ @http_response ||= http_connection.request http_request
64
+ end
65
+ end
66
+ end
@@ -3,14 +3,19 @@ require 'slack-notifier'
3
3
  module CircleciDeploymentNotifier
4
4
  ##
5
5
  # Sends notifications to Slack.
6
- # Builds the message from a BuildInfo object.
6
+ # Builds the message using a BuildInfo object.
7
7
  class Slack
8
+ ##
9
+ # @param webhook_url [String] Slack Webhook URL
10
+ # @param app_name [String] Name of the application that was deployed.
11
+ # @param build_info [BuildInfo]
8
12
  def initialize(webhook_url:, app_name:, build_info:)
9
13
  self.webhook_url = webhook_url
10
14
  self.app_name = app_name
11
15
  self.build_info = build_info
12
16
  end
13
17
 
18
+ # Sends the message to Slack.
14
19
  def send
15
20
  slack_notifier.post message
16
21
  end
@@ -1,3 +1,3 @@
1
1
  module CircleciDeploymentNotifier
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: circleci_deployment_notifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robin Daugherty
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-14 00:00:00.000000000 Z
11
+ date: 2017-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '12.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '12.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '2.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: codacy-coverage
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: slack-notifier
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -98,12 +112,14 @@ description:
98
112
  email:
99
113
  - robin@robindaugherty.net
100
114
  executables:
115
+ - circleci_deployment_notify_new_relic
101
116
  - circleci_deployment_notify_slack
102
117
  extensions: []
103
118
  extra_rdoc_files: []
104
119
  files:
105
120
  - ".gitignore"
106
121
  - ".rspec"
122
+ - ".rubocop.yml"
107
123
  - ".travis.yml"
108
124
  - Gemfile
109
125
  - LICENSE.txt
@@ -112,10 +128,12 @@ files:
112
128
  - bin/console
113
129
  - bin/setup
114
130
  - circleci_deployment_notifier.gemspec
131
+ - exe/circleci_deployment_notify_new_relic
115
132
  - exe/circleci_deployment_notify_slack
116
133
  - lib/circleci_deployment_notifier.rb
117
134
  - lib/circleci_deployment_notifier/build.rb
118
135
  - lib/circleci_deployment_notifier/build_info.rb
136
+ - lib/circleci_deployment_notifier/new_relic.rb
119
137
  - lib/circleci_deployment_notifier/slack.rb
120
138
  - lib/circleci_deployment_notifier/version.rb
121
139
  homepage: https://github.com/RobinDaugherty/circleci_deployment_notifier