【问题标题】:Host Rails App in the Google Cloud App Engine and set Environment variables在 Google Cloud App Engine 中托管 Rails 应用并设置环境变量
【发布时间】:2019-04-23 01:12:07
【问题描述】:
我将在 Google App-Engine 中托管我的 RoR API-App。
到目前为止一切正常,但我必须将用户名、密码和密钥(例如数据库用户/密码)以纯文本形式存储在 app.yaml 中。这太愚蠢了,所以我永远无法将它推送到我的 git 仓库!通常我将这样的东西存储在一个环境变量中并在我的应用程序中使用它们。
但我没有找到设置或访问环境变量的方法。
有没有办法或替代方法?
【问题讨论】:
标签:
ruby-on-rails
ruby
google-app-engine
deployment
environment-variables
【解决方案2】:
我做到了!
对于本地开发,我只是像往常一样设置我的环境。
如果模式是生产模式,我会从 Google Datastore 所有键值对加载它们并将它们设置为环境变量。
我在初始化程序中执行此操作,为此只需在YourApp/config/initializers/ 中创建一个文件并将代码放入其中!只需创建新实体,复制代码中的 Kind 名称并设置您的项目 ID。由于您的应用托管在 Google 中,因此它应该可以访问数据存储区(您需要在 IAM-Manager 中设置权限)
require "google/cloud/datastore"
# Load the enviroment variables from the google datastore!
if Rails.env == "production"
data_store = Google::Cloud::Datastore.new(
project_id: 'YOUR_PROJECT_ID'
)
query = data_store.query "YOUR_KIND_NAME"
results = data_store.run query
puts "Set custom env variables!"
# Set each result as an env variable
results[0].properties.to_h.each do |key, value|
ENV[key]= value
end
end