ENV变量中的Capistrano和API键? [英] Capistrano and API keys in ENV variables?
问题描述
在环境变量中设置API密钥似乎是理想的解决方案,但是我无法使用 ENV [ SOME_KEY]
。
根据这篇文章,因为capistrano运行作为非互动和非登录,〜/ .bashrc
和〜/ .bash_profile
未加载。流程图表明我应该使用 $ BASH_ENV
。
我可以将我的api密钥添加到 $ BASH_ENV
并在我的rails应用程序和使用 ENV [SOME_KEY]
的守护进程的ruby文件中访问它们
我还在想,只需将api密钥添加到服务器上的某个文件,并将其符号链接到ruby文件目录和rails目录,然后打开并阅读它。这是可能的吗?
根据我的经验,有几种方法可以与Capistrano合作。
rbenv-vars
如果您通过Rbenv在您的服务器上使用Ruby,那么你会幸运。有一个名为 rbenv-vars 的Rbenv插件会自动将环境变量注入到任何Ruby进程中,包括您的Rails应用程序只需使用 KEY = value
语法将您的变量添加到服务器上的〜/ .rbenv / vars
这是它。
dotenv
dotenv gem是一个类似的解决方案,但它可以作为您添加到Rails应用程序中的宝石,而不需要Rbenv或任何其他支持工具。将 dotenv-rails
添加到您的Gemfile并部署。 Dotenv将自动在Rails应用程序的根目录中查找 .env.production
文件。对于Capistrano,在Capistrano的共享
目录中的服务器上创建一个 .env.production
文件,然后添加 .env.production
to :linked_files
。现在每个部署都将链接到它。使用 KEY = value
语法声明变量。
.bashrc
使用 export KEY = value
在〜/ .bashrc
文件在服务器上。在Ubuntu中,即使在非交互式SSH会话期间也会评估此文件。在这种情况下,请确保将声明放在顶部:
#如果不是以交互方式运行,不要做任何
case $ - 在
* i *);;
*)return ;;
esac
CentOS可能是一个不同的故事,所以YMMV。
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.
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"]
.
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
.
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"]
?
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?
There are a few ways that work well with Capistrano, in my experience.
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
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
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 may be a different story, so YMMV.
这篇关于ENV变量中的Capistrano和API键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!