In an normal release, Rails app’s unit testing will avoid most errors. But for the urgent code publishing, we have got several time of typo error. Code syntax error may cause server crash for a little while(Passenger web server and we using ./tmp/restart.txt
to restart). We use Capistrano to publish code, so I plan to add a syntax checking before publishing code.
The method is writing a task to bundle exec rails runner, this will report most ruby syntax error(except the undef variables in some functions, runner will load .rb files).
namespace :app do
desc "check all the ruby code"
task :check => :environment do
res = `RAILS_ENV=#{Rails.env} bundle exec rails runner "" 2>&1`
raise res if res.size > 0
end
end
then add this in the deploy.rb (Capistrano 3.1):
namespace :deploy do
task :run_code_check do
on roles(:all) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :rake, 'app:check'
end
end
end
end
before "deploy:updated", "deploy:run_code_check"
end
This is not a tricky part, but please pay attention to the line:
res = `RAILS_ENV=#{Rails.env} bundle exec rails runner "" 2>&1`
This line of code cost me some time, I forget the 2>&1
. so res
will just got the stdout, not the stderr output, which causes the exception is not raised, and Capistrano flow is not stopped.