使用Figaro和Secrets.yml管理环境变量 [英] Using Figaro and Secrets.yml to Manage Env Variables

查看:103
本文介绍了使用Figaro和Secrets.yml管理环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails 4.1应用程序,并且正在尝试组织我的env变量.截至目前,我的config/文件夹中有一个secrets.yml文件.我还安装了figaro宝石.我的目标是将所有env变量都放在application.yml(未检入git)文件中,然后使用secrets.yml(已检入git)文件将变量从appliation.yml映射到应用程序.当我使用Rails.application.secrets打印文件时,它仅显示如下所示的哈希值:

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']"

我的所有外部服务均未使用此环境变量设置.当我查看跟踪时,实际的ENV ['ACCOUNT_ID']正在这样的请求中传递:

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

此外,我无法使用我的应用程序中的Rails.application.secrets.account_id访问环境变量.

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

推荐答案

gem提供了一个生成器:

The gem provides a generator:

$ rails generate figaro:install

生成器创建一个 config/application.yml 文件,并修改 .gitignore 文件,以防止将该文件检入git存储库.

The generator creates a config/application.yml file and modifies the .gitignore file to prevent the file from being checked into a git repository.

您可以将环境变量作为键/值对添加到 config/application.yml :

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

GMAIL_USERNAME: Your_Username

环境变量将在您的应用程序中的任何位置以ENV变量的形式提供:

The environment variables will be available anywhere in your application as ENV variables:

ENV["GMAIL_USERNAME"]

这为您提供了在代码中使用相同变量的便利,无论它们是由Unix shell还是figaro gem的 config/application.yml 设置的. config/application.yml 文件中的变量将覆盖Unix shell中设置的环境变量.

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.

在测试或其他可能不适合使用ENV变量的情况下,您可以通过Figaro方法调用来访问配置值:

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

在这种情况下,ENV["HELLO"]将在开发中产生开发人员",在生产中产生用户",否则将产生世界".

In this case, ENV["HELLO"] will produce "developers" in development, "users" in production and "world" otherwise.

这篇关于使用Figaro和Secrets.yml管理环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆