Using Figaro and Secrets.yml to Manage Env Variables

为君一笑 提交于 2019-12-03 20:11:46

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"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!