Search code examples
node.jsdeploymentcapistranoairbrake

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

?


Solution

  • 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