This week I 'capified' a Rails application I've been developing. It's a basic Rails 4 app using Git for version control and is deployed across multiple environments: development-integration, test-integration, and production. Along with Capistrano's documentation, there are quite a few Capistrano 3 setup/configuration tutorials on the web that are very helpful to get basic deployments working across multiple environments.
Basic Capistrano setup steps consist of:
- Update Gemfile to include capistrano gems:
gem 'capistrano', '~> 3.1' gem 'capistrano-rails', '~> 1.1'
- Run Capistrano install task:
bundle exec cap install
- Configure Capfile to require what you need (asset precompilation, migrations, etc):
require 'capistrano/bundler' require 'capistrano/rails/assets' require 'capistrano/rails/migrations'
- Configure environment specific information in the deploy/<env>.rb files.
Unfortunately, Capistrano doesn't provide a simple environment flag to turn on/off precompiling assets per environment. Out of the box, it's all or nothing. However, you can clear the existing 'assets_precompile' task provided by capistrano and then re-define this task in deploy.rb.
# deploy.rb namespace :deploy do Rake::Task['deploy:compile_assets'].clear desc 'Compile assets' task :compile_assets => [:set_rails_env] do unless fetch(:rails_env) == 'development-integration' invoke 'deploy:assets:precompile' invoke 'deploy:assets:backup_manifest' end end end
Thankfully the provided 'compile_assets' task is only ~4 lines of code making it easy to re-define. The conditional statement above is just a simple example of how to check the rails environment. This could also be accomplished via a custom flag set in each of the deploy/<env>.rb files and checking that custom flag in your custom 'compile_assets' task.