I am setting the below environment variable.
ENV["RAILS_RELATIVE_URL_ROOT"] = "/prefix"
Is there any way of accessing this env variable other than ENV["RAILS_RELATIVE_URL_ROOT"]?
I know rails env can be accesses like Rails.env. Can all the env variables be accesses like that or is it something special for rails_env?
Rails.env only gives you the value of ENV["RAILS_ENV"] or ENV["RACK_ENV"]. If none of them were set, it returns "development".
The ENV object is provided by Ruby.
For another way to access it, you might consider either implementing it by yourself, or using the figaro gem. Set environment variables in a YAML config. Then they can be accessed like this:
Figaro.env.rails_relative_url_root
Related
I need to access a github actions env variable from within a Rails config initializer. Is this possible? I'm specifically looking for env.CI. Is there a way to call that directly in Ruby, or do I need to require the yml file in the initializer and parse it there?
I use ENV variables for all environments to set up different components of the stack, i.e. Redis, Memcached, etc.
I load all of these in the config/application.rb file, and before that I ensure that all environment variables are present.
I'm running into a problem now where I run a rake task before these variables are set, and so it fails my test. Rake seems to doing it's share correctly. This leads me to believe all of these variables initializations are in the wrong spot.
Now I'm at a loss as to the best place to instantiate all these services or check for their existence.
Init them right after Bundler.require(*Rails.groups) in your application.rb like this https://github.com/bkeepers/dotenv#note-on-load-order
You can check env variables in Rails console, for example:
ENV['YOUR_ENV_VARIABLE']
I have a rails 4.1 app and I'm trying to organize my env variables. As of right now I have a secrets.yml file in my config/ folder. I also installed the figaro gem. My goal was to have all my env variables in the application.yml (not checked into git) file and then use the secrets.yml (checked into git) file to map the variables from appliation.yml to the application. When I print the files using Rails.application.secrets It just shows hashes that look like this:
:salesforce_username=>"ENV['SALESFORCE_USERNAME']"
None of my external services are working with this env variables setup. When I view the traces, the actually ENV['ACCOUNT_ID'] are being passed through in the requests like this:
v2/accounts/ENV['ACCOUNT_ID']/envelopes
In addition, I cannot access my env variables using Rails.application.secrets.account_id in my app.
secrets.yml
development:
account_id: <%= ENV['ACCOUNT_ID'] %>
aplication.yml
development:
ACCOUNT_ID: "123456"
application.rb
# preload tokens in application.yml to local ENV
config = YAML.load(File.read(File.expand_path('../application.yml', __FILE__)))
config.merge! config.fetch(Rails.env, {})
config.each do |key, value|
ENV[key] = value.to_s unless value.kind_of? Hash
end
The gem provides a generator:
$ rails generate figaro:install
The generator creates a config/application.yml file and modifies the .gitignore file to prevent the file from being checked into a git repository.
You can add environment variables as key/value pairs to config/application.yml:
GMAIL_USERNAME: Your_Username
The environment variables will be available anywhere in your application as ENV variables:
ENV["GMAIL_USERNAME"]
This gives you the convenience of using the same variables in code whether they are set by the Unix shell or the figaro gem’s config/application.yml. Variables in the config/application.yml file will override environment variables set in the Unix shell.
In tests or other situations where ENV variables might not be appropriate, you can access the configuration values as Figaro method calls:
Figaro.env.gmail_username
Use this syntax for setting different credentials in development, test, or production environments:
HELLO: world
development:
HELLO: developers
production:
HELLO: users
In this case, ENV["HELLO"] will produce “developers” in development, “users” in production and “world” otherwise.
You say the ENV variables are being "passed through in the requests", but when I look at your code snippets I think the variables aren't ever being detected as such in the first place.
If you want to inject a variable into a string, double-check that you are using the following format, especially the # and {}:
important_string = "v2/accounts/#{ENV['ACCOUNT_ID']}/envelopes"
On a more general note, if you're unsure what environment variables are being set in a given environment, the easiest way to double-check is to open up the Rails console and query ENV like so:
$ rails console
> puts ENV.keys # find out what ENV vars are set
=> (returns a long list of var names)
> puts ENV['DEVISE_PEPPER']
=> "067d793e8781fa02aebd36e239c7878bdc1403d6bcb7c380beac53189ff6366be"
Here's how I set my ENV variable
#~/repos/project
export GA_USERNAME="email#gmail.com"
When I try to use ENV['GA_USERNAME] in my application, it returns nil. However, in the console it strangely prints the variable and then returns nil...
1.9.2-p320 :003 > puts ENV['GA_USERNAME']
email#gmail.com
=> nil
Edit I don't think my ENV variables are persisting. When I restart my computer, variable is nil and needs to be set again.
A traditional way to set env vars for a server process is:
RAILS_ENV=production GA_USERNAME=foobar rails s
If you want to specify the env vars in a persistent way, like in a file, try figaro. The README tells you how to start, but the idea is:
rails generate figaro:install
edit config/application.yml (which the last command created)
in your app, it will automatically set per-environment (production, development, test) variables
UPDATE
dotenv might be an even better and simpler approach:
gem 'dotenv-rails', :groups => [:development, :test]
put cross-environment key-value pairs in .env
put env-specific pairs .env.test or .env.development
See their README for all the details.
In my web application I want to access environment variables inside my initializers file.
My intiailizer file looks like(initializers/gcm_on_rails.rb)
configatron.gcm_on_rails.api_url = 'https://android.googleapis.com/gcm/send'
#configatron.gcm_on_rails.api_key = 'abc'
configatron.gcm_on_rails.api_key == ENV['GCM_API_KEY']
Rails.logger.debug"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ #{configatron.gcm_on_rails.api_key}"
configatron.gcm_on_rails.app_name = 'com.xyz'
configatron.gcm_on_rails.delivery_format = 'json'
and In my .bashrc file I added following key value
export GCM_API_KEY="abc"
but my logger not giving me any value. that means I am not able to access those environment variables. I want to access those variables. Need Help... Thank you.....
If you’ve set environment variables in your /etc/bashrc or /etc/profile, then these environment variables are made available in your shell. However, on most operating systems, Apache is not started from the shell and does not load environment variables defined in bashrc/profile, which is why setting environment variables in /etc/bashrc and /etc/profile usually has no effect on Apache (and by induction, on Passenger and Rails processes).
Ref:- http://blog.phusion.nl/2008/12/16/passing-environment-variables-to-ruby-from-phusion-passenger/