Skip to content

Instantly share code, notes, and snippets.

@ianmurrays
Created July 21, 2011 17:26
Show Gist options
  • Save ianmurrays/1097695 to your computer and use it in GitHub Desktop.
Save ianmurrays/1097695 to your computer and use it in GitHub Desktop.
Runs test locally before deploying on capistrano.
set :test_log, "logs/capistrano.test.log"
namespace :deploy do
before 'deploy:update_code' do
puts "--> Running tests, please wait ..."
unless system "bundle exec rake > #{test_log} 2>&1" #' > /dev/null'
puts "--> Tests failed. Run `cat #{test_log}` to see what went wrong."
exit
else
puts "--> Tests passed"
system "rm #{test_log}"
end
end
end
@jondavidjohn
Copy link

good call, thanks for this!

@cheshire137
Copy link

With Capistrano 3, I did before :deploy, "deploy:run_tests" and defined a run_tests task like on https://github.com/TalkingQuickly/capistrano-3-rails-template/blob/master/lib/capistrano/tasks/run_tests.cap

@brunomperes
Copy link

Probably what @cheshire137 did, but I had to do some adjustments: http://stackoverflow.com/a/37708034/2087198

@thomasdarde
Copy link

Here is an updated version for capistrano3 / sshkit

namespace :deploy do
  desc "Runs test before deploying, can't deploy unless they pass"
  task :run_tests do
    run_locally do
      test_log = "log/capistrano.test.log"
      tests = fetch(:tests)
      tests.each do |test|
        info "--> Running tests locally: '#{test}', please wait ..."
        unless test(:rspec, "--tag ~js #{test}  > #{test_log} 2>&1")
          warn "--> Tests: '#{test}' failed. Results in: #{test_log} and below:"
          execute :cat, test_log
          exit;
        end
        info "--> '#{test}' passed"
      end
      info "--> All tests passed"
      execute :rm, test_log
    end
  end
  before :deploy, "deploy:run_tests"
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment