ENV变量中的Capistrano和API密钥? [英] Capistrano and API keys in ENV variables?

查看:22
本文介绍了ENV变量中的Capistrano和API密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Capistrano 部署我的 rails 应用程序.我想将服务器上的一些 API 密钥保存为环境变量.我使用 Capistrano 部署的 Rails 应用程序应该可以访问这些 API 密钥.作为守护进程运行的单独的 ruby​​ 文件也应该可以访问这些 API 密钥.

I'm deploying my rails app with Capistrano. I want to save some API keys on the server as an environment variable. Those API keys should be accessible to my rails app that is deployed with Capistrano. Those API keys should also be accessible to a separate ruby file that is run as a daemon.

在环境变量中设置 API 密钥似乎是理想的解决方案,但是,我无法在我的 rails 应用程序中使用 ENV["SOME_KEY"] 访问它们.

setting the API keys in environment variables seems like the ideal solution, however, I can't access them in my rails app with ENV["SOME_KEY"].

根据 这篇文章,因为 capistrano 以非交互和非登录方式运行,~/.bashrc~/.bash_profile 没有加载.流程图建议我应该使用 $BASH_ENV.

According to this post, because capistrano runs as non interactive and non login, ~/.bashrc and ~/.bash_profile are not loaded. The flowchart suggests that I should use $BASH_ENV.

我可以在 $BASH_ENV 中添加我的 api 密钥,然后在我的 rails 应用程序和带有 ENV["SOME_KEY"] 的守护程序的 ruby​​ 文件中访问它们?

Can I just add my api keys in $BASH_ENV and access them in my rails app and in the ruby file that is a daemon with ENV["SOME_KEY"]?

我还在考虑将 api 密钥添加到服务器某处的文件中,并将其符号链接到 ruby​​ 文件目录和 rails 目录,然后打开并读取它.这可能吗?

I'm also thinking of just adding the api keys to a file somewhere on the server and symlinking it to the ruby file dir and rails dir and just open and reading it. Would this be possible?

推荐答案

根据我的经验,有几种方法可以很好地与 Capistrano 配合使用.

There are a few ways that work well with Capistrano, in my experience.

rbenv-vars

如果您在服务器上通过 Rbenv 使用 Ruby,那么您很幸运.有一个名为 rbenv-vars 的 Rbenv 插件可以自动将环境变量注入任何 Ruby 进程,这将包括您的 Rails 应用程序.只需使用 KEY=value 语法将变量添加到服务器上的 ~/.rbenv/vars 即可.就是这样.

If you use Ruby via Rbenv on your server, then you are in luck. There is a Rbenv plugin called rbenv-vars that automatically injects environment variables into any Ruby process, which would include your Rails app. Just add your variables to ~/.rbenv/vars on the server using KEY=value syntax. That's it.

dotenv

dotenv gem 是一个类似的解决方案,但它可以作为添加到 Rails 应用程序的 gem并且不需要 Rbenv 或任何其他支持工具.将 dotenv-rails 添加到您的 Gemfile 并进行部署.Dotenv 将自动在 Rails 应用程序的根目录中查找 .env.production 文件.对于 Capistrano,在 Capistrano 的 shared 目录内的服务器上创建一个 .env.production 文件,然后将 .env.production 添加到 :linked_files.现在每个部署都将链接到它.使用 KEY=value 语法声明变量.

The dotenv gem is a similar solution, but it works as a gem you add to your Rails app and doesn't require Rbenv or any other supporting tools. Add dotenv-rails to your Gemfile and deploy. Dotenv will automatically look for a .env.production file in the root of your Rails app. For Capistrano, create a .env.production file on the server inside Capistrano's shared directory, and then add .env.production to :linked_files. Now every deploy will link to it. Declare your variables using KEY=value syntax.

.bashrc

在服务器上 ~/.bashrc 文件的最顶部使用 export KEY=value 语法声明您的变量.在 Ubuntu 上,即使在非交互式 SSH 会话期间也会评估此文件.只需确保将声明放在顶部,在此 case 语句之前:

Declare your variables with export KEY=value syntax at very top of the ~/.bashrc file on the server. On Ubuntu, this file is evaluated even during an non-interactive SSH session. Just make sure you place the declarations on the top, before this case statement:

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

CentOS 可能是另一回事,所以 YMMV.

CentOS may be a different story, so YMMV.

这篇关于ENV变量中的Capistrano和API密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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