Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a Rails environment variable in Capistrano deploy script?

If I have a constant in my Rails environment.rb, for example...

SOME_CONSTANT = 3

Is it possible to access this in my capistrano deploy.rb somehow? It seems simple but I can't figure out how.

like image 408
Brian Armstrong Avatar asked Nov 26 '09 19:11

Brian Armstrong


People also ask

How do I run Capfile?

Navigate to your application's root directory in Terminal and run the following command: capify . This command creates a special file called Capfile in your project, and adds a template deployment recipe at config/deploy. rb in your Rails project.

What is Capistrano gem?

Capistrano is a utility and framework for executing commands in parallel on multiple remote machines, via SSH.


3 Answers

This ended up working:

created a file config/initializers/my_constant.rb

put my constant in there (rails automatically loads files there so I can use the constant in my app)

then in deploy.rb added load 'config/initializers/my_constant' so it could be used there as well.

like image 139
Brian Armstrong Avatar answered Oct 23 '22 23:10

Brian Armstrong


You should access it via the ENV[] hash (this is a Ruby thing), here is an example using the TERM environmental variable.

puts "Your Terminal is #{ENV['TERM']}"

If you need a ruby constant, from your rails environment, you should load it:

require 'config/environment'

Beware that this will load your whole application environment, you should think to use something like AppConfig, or SimpleConfig (insert other tool here) to store configurations, then you need only load the tool, which processes your config files.

like image 39
Lee Hambley Avatar answered Oct 23 '22 23:10

Lee Hambley


Why not define these constants in a file in lib/ and then require the file in both your Rails app and your Capfile?

like image 22
tadman Avatar answered Oct 23 '22 21:10

tadman