# Setting up staging server - setting environment

**URL:** <https://www.ruby-forum.com/t/setting-up-staging-server-setting-environment/151302>\
**Category:** Deployment\
**Created:** [October 20, 2008, 3:35am UTC](https://www.ruby-forum.com/t/setting-up-staging-server-setting-environment/151302 "2008-10-20T03:35:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [October 20, 2008, 3:35am UTC](https://www.ruby-forum.com/t/setting-up-staging-server-setting-environment/151302/1 "2008-10-20T03:35:44Z")

</div>

Hi,  
I thought it would be easy to get a staging server working. I’m using  
capistrano 2 and the ext gem. I already had everything working with  
capistrano for a production deployment…

I added this to my deploy.rb  
require ‘capistrano/ext/monitor’  
require ‘capistrano/ext/multistage’

I created a /deploy/staging.rb  
set :deploy\_to, “/where-the-staging-stuff-goes”  
set :rails\_env, ‘staging’

I added a “:staging” section in my database.yml file which is the same  
as production section but points to a different database

Most of this is working. The deployment works and I can run the app at  
the new location, but for some reason it continues to use the production  
database rather than the staging one that I have pointed it to with the  
staging section of my database.yml file.

thanks for any words of wisdom on this.

jp

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [October 20, 2008, 4:49am UTC](https://www.ruby-forum.com/t/setting-up-staging-server-setting-environment/151302/2 "2008-10-20T04:49:05Z")

</div>

Hi Jeff,

Within your config/deploy/staging.rb file something like the following  
could help:

require ‘erb’

before “deploy:setup”, :db  
after “deploy:update\_code”, “db:symlink”

namespace :db do  
desc “Create database yaml in shared path”  
task :default do  
db\_config = ERB.new \<\<-EOF  
staging:  
adapter: mysql  
encoding: utf8  
username: #{user}  
password: #{password}  
database: #{application}\_staging  
EOF

```
 run "mkdir -p #{shared_path}/config"
 put db_config.result, "#{shared_path}/config/database.yml"

```

end

desc “Make symlink for database yaml”  
task :symlink do  
run “ln -nfs #{shared\_path}/config/database.yml #{release\_path}/  
config/database.yml”  
end  
end

Regards,  
Keith

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [October 20, 2008, 6:44am UTC](https://www.ruby-forum.com/t/setting-up-staging-server-setting-environment/151302/3 "2008-10-20T06:44:48Z")

</div>

Hi Jeff,

Sorry if I’ve created a red herring for you. What I’ve provided  
probably won’t solve your problem but it would keep your database  
username and password more secure.

Getting back to your problem, have you set up config/environments/  
staging.rb including ENV[‘RAILS\_ENV’] = ‘staging’?

Keith

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [October 20, 2008, 5:29am UTC](https://www.ruby-forum.com/t/setting-up-staging-server-setting-environment/151302/4 "2008-10-20T05:29:30Z")

</div>

Hi Keith,  
I appreciate the help, but you’ve left me a bit confused. If I read  
this right, your addition is just going to add a staging section into  
the database.yml file on the fly during the deploy setup. Seems easier  
to just add it in for real. Please explain what/why.

thanks much,  
jp

Keith P. wrote:

> Hi Jeff,
> 
> Within your config/deploy/staging.rb file something like the following  
> could help:
> 
> require ‘erb’
> 
> before “deploy:setup”, :db  
> after “deploy:update\_code”, “db:symlink”
> 
> namespace :db do  
> desc “Create database yaml in shared path”  
> task :default do  
> db\_config = ERB.new \<\<-EOF  
> staging:  
> adapter: mysql  
> encoding: utf8  
> username: #{user}  
> password: #{password}  
> database: #{application}\_staging  
> EOF
> 
> ```
> run "mkdir -p #{shared_path}/config"
> put db_config.result, "#{shared_path}/config/database.yml"
> 
> ```
> 
> end
> 
> desc “Make symlink for database yaml”  
> task :symlink do  
> run “ln -nfs #{shared\_path}/config/database.yml #{release\_path}/  
> config/database.yml”  
> end  
> end
> 
> Regards,  
> Keith

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [October 20, 2008, 7:24am UTC](https://www.ruby-forum.com/t/setting-up-staging-server-setting-environment/151302/5 "2008-10-20T07:24:05Z")

</div>

Hi Keith,  
Yes, I managed to get it working just a moment ago. This is indeed what  
was missing.

thanks,  
jp

Keith P. wrote:

> Hi Jeff,
> 
> Sorry if I’ve created a red herring for you. What I’ve provided  
> probably won’t solve your problem but it would keep your database  
> username and password more secure.
> 
> Getting back to your problem, have you set up config/environments/  
> staging.rb including ENV[‘RAILS\_ENV’] = ‘staging’?
> 
> Keith

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [October 22, 2008, 12:51pm UTC](https://www.ruby-forum.com/t/setting-up-staging-server-setting-environment/151302/6 "2008-10-22T12:51:32Z")

</div>

In your deploy.rb you should have something like this:

require ‘capistrano/ext/multistage’  
set :stages, %w(production staging)  
set :default\_stage, “”

Then when you call capistrano:

cap staging deploy

Note: In :stages you should write the filename (without extension) of  
your deploy/\*.rb files

Jeff P. escribiÃ³:

> set :deploy\_to, “/where-the-staging-stuff-goes”  
> thanks for any words of wisdom on this.
> 
> jp

–  
Rafael Garcia Ortega
