Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

jondavidjohn commented Mar 8, 2012

good call, thanks for this!

@cheshire137
Copy link

cheshire137 commented Apr 11, 2014

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

brunomperes commented Jun 8, 2016

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

@thomasdarde
Copy link

thomasdarde commented Mar 24, 2017

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