ENV 变量中的 Capistrano 和 API 密钥?

编程入门 行业动态 更新时间:2024-10-22 05:02:52
本文介绍了ENV 变量中的 Capistrano 和 API 密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在使用 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 应用程序和 ruby​​ 文件中访问它们,该文件是一个带有 ENV["SOME_KEY"] 的守护进程?

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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-18 15:29:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/942677.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:密钥   变量   ENV   API   Capistrano

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!