You can use a capistrano hook to create files on the server or symlink them in from e.g. shared/
when deploying.
For Rails 2.3:
On your web host, create the file shared/preinitializer.rb
:
ENV['RAILS_ENV'] = 'staging'
Then add this to your Capfile
(or possibly config/deploy.rb
if you're using a newer version of capistrano with Rails 2.x:
after 'deploy:symlink', 'localize:copy_shared_configurations'
namespace :localize do
desc 'copy shared configurations to current'
task :copy_shared_configurations, :roles => [:app] do
# I put database.yml here to keep deployed db credentials out of git
%w[
preinitializer.rb
].each do |f|
run "ln -nsf #{shared_path}/#{f} #{current_path}/config/#{f}"
end
end
end
For Rails 3
Due to the changes in Rails 3's initialization sequence, config/preinitializer.rb
is not loaded until after config/environment.rb
is loaded. So for Rails 3, you want to modify config/environment.rb
only on the server. You could do this with a similar setup like Rails 2 above, but using a symlinked copy of config/environment.rb
, and adding the step of deleting the existing file before trying to symlink.
Another option would be to overwrite the environment.rb on the server from capistrano. In your config/deploy.rb
:
after 'deploy:symlink', 'localize:munge_environment'
namespace :localize do
desc 'munge environment.rb to set staging environment'
task :munge_environment, :roles => [:app] do
new_env = "ENV['RAILS_ENV'] = 'staging'\n" + File.read(Rails.root.join('config', 'environment.rb'))
put new_env, "#{current_path}/config/environment.rb"
end
end