简体   繁体   中英

How do I successfully notify Airbrake of a deployment when using capistrano to deploy a Node.js project?

This is a bit of an oddball question.

Capistrano 2.14.2

I'm using capistrano to deploy a couple of Node.js projects, and this works fine (from within the same rvm and gemset Ruby installation). However, I'd like to have Airbrake be notified of these deployments.

Using the 'airbrake' Node.js module, and calling

airbrake.trackDeployment({repo: '...'}); 

works, but not sure how to reliably call this just once at deploy time. If I call it within my server, then Airbrake is notified of a "deployment" every time my server starts, which is obviously not correct.

Adding

require 'airbrake/capistrano'

to deploy.rb definitely does not work.

How do others successfully use

airbrake.trackDeployment

?

You could create a simple js file you'd run locally (on your machine for example) that notifies airbrake as a last deploy task. You could for example use the backtick operator to run a task:

deploy.task :notify_airbrake do
    `node notify_airbrake.js`
end

If you don't have node installed locally, you could also pick one of the servers to run the notification script through ssh:

deploy.task :notify_airbrake do
    `ssh youserver "node notify_airbrake.js"`
end

Based on this solution http://dwradcliffe.com/2011/09/26/using-airbrake-with-node.html (which is clearly embedded in a Rails app.), I came up with the following, which depends solely on Javascript:

In my Node.js root directory, create a deploy.js file, like so:

var airbrake = require('airbrake').createClient("AIRBRAKE_API_KEY");

var deployment = {rev: process.argv[2],
                  repo: process.argv[3],
                  env: process.argv[4],
                  user: process.argv[5]};

airbrake.trackDeployment(deployment, function(err, params) {
  if (err) {throw err}
  console.log('Tracked deployment of %s to %s', params.rev, params.env);
})

In config/deploy.rb, add

require 'airbrake/capistrano'

and

namespace :airbrake do
  desc "Notify Airbrake of a new deploy."
  task :deploy do
    system "node deploy.js #{current_revision} #{repository} #{stage} #{user}"
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM