How to run local commands like 'npm test' with Capistrano

Before I deploy, I want to run my test suite locally (executed with npm test). I also want the deployment to stop if the tests fail.

I created a new file in lib/capistrano/tasks. I called it test.rake. Here are the contents:

1
2
3
4
5
6
7
8
9
10
11
12
namespace :test do
desc 'Run Tests'
task :run do
run_locally do
unless system 'npm test'
puts 'Test(s) failed, so the deployment is being aborted.'
exit;
end
end
end
end

Then, in my deploy.rb deploy block, I run the command like this:

1
before :starting, 'test:run'
avatar

Dev Blog