Using Figaro and Secrets.yml to manage Env variables

I have a rails 4.1 application and I am trying to organize my env variables. At the moment I have a secrets.yml file in my config / folder. I also installed figaro gem. My goal was to have all my env variables in the application.yml file (not checked in the git file) and then use the secrets.yml file (registered in git) to map the from appliation.yml variables to the application. When I print files using Rails.application.secrets, it just shows hashes that look like this:

:salesforce_username=>"ENV['SALESFORCE_USERNAME']"

      

None of my external services work with this setting of env variables. When I view the traces, actually ENV ['ACCOUNT_ID'] is being passed in requests like this:

v2/accounts/ENV['ACCOUNT_ID']/envelopes

      

Also, I am unable to access my env variables using Rails.application.secrets.account_id in my application.

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

      

+3


source to share


2 answers


The gem provides the 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 out in the git repository.

You can add environment variables as key / value pairs in config / application.yml :

GMAIL_USERNAME: Your_Username

      

Environment variables will be available anywhere in your application as ENV variables:

ENV["GMAIL_USERNAME"]

      



This gives you the ability to use the same variables in your code, whether they are set by the Unix shell or the figstones config / application.yml . The variables in the config / application.yml file will override the environment variables set in the Unix shell.

In tests, or in other situations where ENV variables might not be acceptable, you can access the configuration values ​​caused by calls to Figaro's method:

Figaro.env.gmail_username

      

Use this syntax to set different credentials in a development, test, or production environment:

HELLO: world
development:
  HELLO: developers
production:
  HELLO: users

      

In this case, it ENV["HELLO"]

will create "developers" in development, "users" in production, and "world" otherwise.

+11


source


You say that ENV variables are "passed in requests", but when I look at the code snippets, I think the variables are never shown up as such in the first place.

If you want to insert a variable into a string, double check that you are using the following format, in particular #

, and {}

:

important_string = "v2/accounts/#{ENV['ACCOUNT_ID']}/envelopes"

      



More generally, if you don't know which environment variables are set in a given environment, the easiest way to double check is to open the Rails console and query ENV

like this:

$ rails console
> puts ENV.keys # find out what ENV vars are set
=> (returns a long list of var names)
> puts ENV['DEVISE_PEPPER']
=> "067d793e8781fa02aebd36e239c7878bdc1403d6bcb7c380beac53189ff6366be"

      

-1


source







All Articles