Skip to content

hiding production database info from source control with capistrano

Whether you are working on an open source app or an enterprise app, it is a good idea to keep the production database connection information out of source control. To do so, add the following to your deploy.rb file:

task :overwrite_database_yml_file do
  run "[ -f /var/rails/#{application}/database.yml ] && cp -f /var/rails/#{application}/database.yml #{latest_release}/config/ || echo 'database.yml was not overwritten'"
end
after "deploy:update_code", :overwrite_database_yml_file

And place a database.yml file containing only your production connection information in the same directory as current and shared (eg the deploy_to directory). In fact I typically take the production information out of the config/database.yml entirely.

When deploy runs, if it finds a database.yml file in the deploy_to directory, it will copy it into config before migrating or starting the application. You can use this task to copy other configuration files that you want to keep out of source control as well, but I think its a good idea to only have the database connect info and host names change between deployments.

Post a Comment

Your email is never published nor shared.