├── .github └── workflows │ └── test.yml ├── .gitignore ├── .rubocop.yml ├── CODE_OF_CONDUCT.md ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── bin ├── console └── setup ├── capistrano-sentry.gemspec ├── lib ├── capistrano-sentry.rb └── capistrano │ ├── sentry.rb │ ├── sentry │ └── version.rb │ └── tasks │ └── sentry.rake └── test ├── capistrano └── sentry_test.rb └── test_helper.rb /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: [push] 4 | 5 | jobs: 6 | test: 7 | 8 | runs-on: ubuntu-latest 9 | 10 | strategy: 11 | matrix: 12 | ruby: [ '2.6' ] 13 | 14 | steps: 15 | - uses: actions/checkout@v2 16 | 17 | - name: Set up Ruby ${{ matrix.ruby }} 18 | uses: actions/setup-ruby@v1 19 | with: 20 | ruby-version: ${{ matrix.ruby }} 21 | 22 | - name: Retrieve gems cache 23 | uses: actions/cache@v1.1.2 24 | with: 25 | path: vendor/bundle 26 | key: ${{ runner.os }}-${{ matrix.ruby }}-gem-${{ hashFiles('**/Gemfile.lock') }} 27 | 28 | - name: Install gems 29 | run: | 30 | rm -f .ruby-version 31 | bundle config path vendor/bundle 32 | bundle install --jobs 4 --retry 3 33 | 34 | - name: Run tests 35 | run: bundle exec rake test 36 | 37 | - name: Upload log if failure 38 | uses: actions/upload-artifact@v1 39 | if: failure() 40 | with: 41 | name: test.log 42 | path: log/test.log 43 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /_yardoc/ 4 | /coverage/ 5 | /doc/ 6 | /pkg/ 7 | /spec/reports/ 8 | /tmp/ 9 | Gemfile.lock 10 | *~ 11 | -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | AllCops: 2 | TargetRubyVersion: 2.4 3 | 4 | Layout/AccessModifierIndentation: 5 | EnforcedStyle: outdent 6 | IndentationWidth: 2 7 | 8 | Layout/ArgumentAlignment: 9 | EnforcedStyle: with_fixed_indentation 10 | IndentationWidth: 2 11 | 12 | Layout/FirstArrayElementIndentation: 13 | EnforcedStyle: consistent 14 | 15 | Layout/FirstHashElementIndentation: 16 | EnforcedStyle: consistent 17 | 18 | Layout/HashAlignment: 19 | EnforcedHashRocketStyle: table 20 | EnforcedColonStyle: table 21 | 22 | Layout/MultilineMethodCallIndentation: 23 | EnforcedStyle: indented 24 | 25 | Layout/ParameterAlignment: 26 | EnforcedStyle: with_fixed_indentation 27 | IndentationWidth: 2 28 | 29 | Lint/RaiseException: 30 | Enabled: true 31 | 32 | Lint/StructNewOverride: 33 | Enabled: true 34 | 35 | Metrics/AbcSize: 36 | Max: 65 37 | 38 | Metrics/BlockLength: 39 | Max: 70 40 | 41 | Metrics/ClassLength: 42 | Max: 300 43 | 44 | Metrics/CyclomaticComplexity: 45 | Max: 10 46 | 47 | Metrics/MethodLength: 48 | Max: 60 49 | 50 | Metrics/ModuleLength: 51 | Max: 300 52 | 53 | Metrics/PerceivedComplexity: 54 | Max: 10 55 | 56 | Naming/FileName: 57 | Enabled: false 58 | 59 | Style/AsciiComments: 60 | Enabled: false 61 | 62 | Style/ClassAndModuleChildren: 63 | AutoCorrect: true 64 | 65 | Style/ConditionalAssignment: 66 | Enabled: false 67 | 68 | Style/Documentation: 69 | Enabled: false 70 | 71 | Style/EmptyMethod: 72 | EnforcedStyle: expanded 73 | 74 | Style/GuardClause: 75 | MinBodyLength: 3 76 | 77 | Style/HashEachMethods: 78 | Enabled: true 79 | AutoCorrect: true 80 | 81 | Style/HashTransformKeys: 82 | Enabled: true 83 | AutoCorrect: true 84 | 85 | Style/HashTransformValues: 86 | Enabled: true 87 | AutoCorrect: true 88 | 89 | Style/IfUnlessModifier: 90 | Enabled: false 91 | 92 | Style/MultipleComparison: 93 | Enabled: false 94 | 95 | Style/NumericPredicate: 96 | Enabled: false 97 | 98 | Style/SymbolArray: 99 | MinSize: 7 100 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at brice@codeur.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at [http://contributor-covenant.org/version/1/4][version] 72 | 73 | [homepage]: http://contributor-covenant.org 74 | [version]: http://contributor-covenant.org/version/1/4/ 75 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | source 'https://rubygems.org' 4 | 5 | git_source(:github) { |repo_name| "https://github.com/#{repo_name}" } 6 | 7 | # Specify your gem's dependencies in capistrano-sentry.gemspec 8 | gemspec 9 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Brice Texier 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Capistrano::Sentry 2 | 3 | Simple extension of capistrano for automatic notification of Sentry. 4 | 5 | ## Installation 6 | 7 | Add this line to your application's Gemfile: 8 | 9 | ```ruby 10 | gem 'capistrano-sentry', require: false 11 | ``` 12 | 13 | Then, add this line to your application's Capfile: 14 | 15 | ```ruby 16 | require 'capistrano/sentry' 17 | ``` 18 | 19 | And then execute from your command line: 20 | 21 | ```bash 22 | bundle 23 | ``` 24 | 25 | ## Usage 26 | 27 | Add these lines to your application's `config/deploy.rb`: 28 | 29 | ```ruby 30 | # Sentry deployment notification 31 | set :sentry_host, 'https://my-sentry.mycorp.com' # https://sentry.io by default 32 | set :sentry_api_token, '0123456789abcdef0123456789abcdef' 33 | set :sentry_organization, 'my-org' # fetch(:application) by default 34 | set :sentry_project, 'my-proj' # fetch(:application) by default 35 | set :sentry_repo, 'my-org/my-proj' # computed from repo_url by default 36 | ``` 37 | 38 | If you want deployments to be published in every Rails environment, put this in `config/deploy.rb`, otherwise put it your environment-specific deploy file (i.e. `config/deploy/production.rb`): 39 | ```ruby 40 | before 'deploy:starting', 'sentry:validate_config' 41 | after 'deploy:published', 'sentry:notice_deployment' 42 | ``` 43 | 44 | ### Explaination of Configuration Properties 45 | 46 | * `sentry_host`: identifies to which host Sentry submissions are sent. [https://sentry.io by default] 47 | 48 | * `sentry_api_token`: API Auth Tokens are found/created in you Sentry Account Settings (not in the organization or project): `Settings > Account > Api > Auth Tokens`. 49 | [https://sentry.io/settings/account/api/auth-tokens/] 50 | 51 | * `sentry_organization`: The "**Name**" ("*A unique ID used to identify this organization*") from Sentry's Organization Settings page. 52 | [https://sentry.io/settings/{ORGANIZATION_SLUG}] 53 | 54 | * `sentry_project`: The "**Name**" ("*A unique ID used to identify this project*") from Sentry's Project Settings page. 55 | [https://sentry.io/settings/{ORGANIZATION_SLUG}/projects/{PROJECT_SLUG}] 56 | 57 | * `sentry_repo`: The `repository` name to be used when reporting repository details to Sentry [computed from `fetch(:repo_url)` by default -- `https://github.com/codeur/capistrano-sentry` becomes `//github.com/codeur/capistrano-sentry` and `git@github.com:codeur/capistrano-sentry.git` becomes `codeur/capistrano-sentry`] 58 | 59 | * `sentry_repo_integration`: this enables/disables submission of git repo information (`release_refs` below) to Sentry [Enabled (`true`) by default]. 60 | 61 | * `sentry_release_refs`: Repository details about this realease (`repository`, `commit`, `previousCommit`) to Sentry [computed from `sentry_repo`, `current_revision`, and `previous_revision`)]. 62 | 63 | * `sentry_release_version`: Version number (tag, etc.) used to identify this release to Sentry [computed from `current_revision` or repository `HEAD`)]. 64 | 65 | * `deploy_name`: A name (revision, version number, tag, etc.) used to identify this release deploy to Sentry [computed from `sentry_release_version`+`fetch(:release_timestamp)`)]. 66 | 67 | ### Sentry API Documentation 68 | * [Project Releases](https://docs.sentry.io/api/releases/post-project-releases/) 69 | * [Release Deploys](https://docs.sentry.io/api/releases/post-release-deploys/) 70 | 71 | ## Development 72 | 73 | After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. 74 | 75 | To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). 76 | 77 | ## Contributing 78 | 79 | Bug reports and pull requests are welcome on GitHub at https://github.com/codeur/capistrano-sentry. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. 80 | 81 | ## License 82 | 83 | The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). 84 | 85 | ## Code of Conduct 86 | 87 | Everyone interacting in the Capistrano::Sentry project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/codeur/capistrano-sentry/blob/master/CODE_OF_CONDUCT.md). 88 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'bundler/gem_tasks' 4 | require 'rake/testtask' 5 | 6 | Rake::TestTask.new(:test) do |t| 7 | t.libs << 'test' 8 | t.libs << 'lib' 9 | t.test_files = FileList['test/**/*_test.rb'] 10 | end 11 | 12 | task default: :test 13 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | require 'bundler/setup' 5 | require 'capistrano/sentry' 6 | 7 | # You can add fixtures and/or initialization code here to make experimenting 8 | # with your gem easier. You can also use a different console, if you like. 9 | 10 | # (If you use this, don't forget to add pry to your Gemfile!) 11 | # require "pry" 12 | # Pry.start 13 | 14 | require 'irb' 15 | IRB.start(__FILE__) 16 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | set -vx 5 | 6 | bundle install 7 | 8 | # Do any other automated setup that you need to do here 9 | -------------------------------------------------------------------------------- /capistrano-sentry.gemspec: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | lib = File.expand_path('lib', __dir__) 4 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 5 | require 'capistrano/sentry/version' 6 | 7 | Gem::Specification.new do |spec| 8 | spec.name = 'capistrano-sentry' 9 | spec.version = Capistrano::Sentry::VERSION 10 | spec.authors = ['Brice Texier'] 11 | spec.email = ['brice@codeur.com'] 12 | spec.description = 'Sentry release/deployment integration' 13 | spec.summary = 'Push release and deployment information on Sentry on each deploy' 14 | spec.homepage = 'https://github.com/codeur/capistrano-sentry' 15 | spec.license = 'MIT' 16 | 17 | # Specify which files should be added to the gem when it is released. 18 | # The `git ls-files -z` loads the files in the RubyGem that have been added into git. 19 | spec.files = Dir.chdir(File.expand_path(__dir__)) do 20 | `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } 21 | end 22 | spec.test_files = Dir.chdir(File.expand_path(__dir__)) do 23 | `git ls-files -z`.split("\x0").select { |f| f.match(%r{^(test|spec|features)/}) } 24 | end 25 | spec.require_paths = ['lib'] 26 | 27 | spec.add_dependency 'capistrano', '~> 3.1' 28 | 29 | spec.add_development_dependency 'bundler', '~> 1.17' 30 | spec.add_development_dependency 'minitest', '~> 5.0' 31 | spec.add_development_dependency 'rake', '>= 10' 32 | end 33 | -------------------------------------------------------------------------------- /lib/capistrano-sentry.rb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeur/capistrano-sentry/372476da436d2efcf48d5c911c31871c5e9de628/lib/capistrano-sentry.rb -------------------------------------------------------------------------------- /lib/capistrano/sentry.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | load File.expand_path('tasks/sentry.rake', __dir__) 4 | -------------------------------------------------------------------------------- /lib/capistrano/sentry/version.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module Capistrano 4 | module Sentry 5 | VERSION = '0.4.2' 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /lib/capistrano/tasks/sentry.rake: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | # This task will notify Sentry via their API[1][2] that you have deployed 4 | # a new release. It uses the commit hash as the `version` and the git ref as 5 | # the optional `ref` value. 6 | # 7 | # [1]: https://docs.sentry.io/api/releases/post-project-releases/ 8 | # [2]: https://docs.sentry.io/api/releases/post-release-deploys/ 9 | 10 | # For Rails app, this goes in config/deploy.rb 11 | 12 | module Capistrano 13 | class SentryConfigurationError < StandardError 14 | end 15 | end 16 | 17 | namespace :sentry do 18 | desc 'Confirm configuration for notification to Sentry' 19 | task :validate_config do 20 | run_locally do 21 | info '[sentry:validate_config] Validating Sentry notification config' 22 | api_token = ENV['SENTRY_API_TOKEN'] || fetch(:sentry_api_token) 23 | if api_token.nil? || api_token.empty? 24 | msg = 'Missing SENTRY_API_TOKEN. Please set SENTRY_API_TOKEN environment' \ 25 | ' variable or `set :sentry_api_token` in your `config/deploy.rb` file for your Rails application.' 26 | warn msg 27 | raise Capistrano::SentryConfigurationError, msg 28 | end 29 | end 30 | end 31 | 32 | desc 'Notice new deployment in Sentry' 33 | task :notice_deployment do 34 | run_locally do 35 | require 'uri' 36 | require 'net/https' 37 | require 'json' 38 | 39 | head_revision = fetch(:current_revision) || `git rev-parse HEAD`.strip 40 | prev_revision = fetch(:previous_revision) || `git rev-parse #{fetch(:current_revision)}^`.strip 41 | 42 | sentry_host = ENV['SENTRY_HOST'] || fetch(:sentry_host, 'https://sentry.io') 43 | organization_slug = fetch(:sentry_organization) || fetch(:application) 44 | project = fetch(:sentry_project) || fetch(:application) 45 | environment = fetch(:stage) || 'default' 46 | api_token = ENV['SENTRY_API_TOKEN'] || fetch(:sentry_api_token) 47 | repo_integration_enabled = fetch(:sentry_repo_integration, true) 48 | release_refs = fetch(:sentry_release_refs, [{ 49 | repository: fetch(:sentry_repo) || fetch(:repo_url).split(':').last.delete_suffix('.git'), 50 | commit: head_revision, 51 | previousCommit: prev_revision 52 | }]) 53 | release_version = fetch(:sentry_release_version) || head_revision 54 | deploy_name = fetch(:sentry_deploy_name) || "#{release_version}-#{fetch(:release_timestamp)}" 55 | 56 | uri = URI.parse(sentry_host) 57 | http = Net::HTTP.new(uri.host, uri.port) 58 | http.use_ssl = true 59 | 60 | headers = { 61 | 'Content-Type' => 'application/json', 62 | 'Authorization' => 'Bearer ' + api_token.to_s 63 | } 64 | 65 | req = Net::HTTP::Post.new("/api/0/organizations/#{organization_slug}/releases/", headers) 66 | body = { 67 | version: release_version, 68 | projects: [project] 69 | } 70 | body[:refs] = release_refs if repo_integration_enabled 71 | req.body = JSON.generate(body) 72 | response = http.request(req) 73 | if response.is_a? Net::HTTPSuccess 74 | info "Notified Sentry of new release: #{release_version}" 75 | req = Net::HTTP::Post.new( 76 | "/api/0/organizations/#{organization_slug}/releases/#{release_version}/deploys/", 77 | headers 78 | ) 79 | req.body = JSON.generate( 80 | environment: environment, 81 | name: deploy_name 82 | ) 83 | response = http.request(req) 84 | if response.is_a? Net::HTTPSuccess 85 | info "Notified Sentry of new deployment: #{deploy_name}" 86 | else 87 | warn "Cannot notify sentry for new deployment. Response: #{response.code.inspect}: #{response.body}" 88 | end 89 | else 90 | warn "Cannot notify sentry for new release. Response: #{response.code.inspect}: #{response.body}" 91 | end 92 | end 93 | end 94 | end 95 | -------------------------------------------------------------------------------- /test/capistrano/sentry_test.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'test_helper' 4 | require 'capistrano/sentry/version' 5 | 6 | module Capistrano 7 | class SentryTest < Minitest::Test 8 | def test_that_it_has_a_version_number 9 | assert !::Capistrano::Sentry::VERSION.nil? 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | $LOAD_PATH.unshift File.expand_path('../lib', __dir__) 4 | require 'capistrano/sentry' 5 | 6 | require 'minitest/autorun' 7 | --------------------------------------------------------------------------------