环境变量在本地和Heroku [英] Environment variables locally and Heroku
问题描述
模块MyConfig
def config
environment = ENV [RACK_ENV] || 开发
YAML.load_file(./ config / config.yml)[environment]
end
end
所以当我想使用一个变量我做这个例如
aws_access_key_id = config ['aws_access_key']
我有一个.gitignore文件,在推送时忽略config.yml以github为例,当我推到heroku这些环境变量将无法访问?
所以这让我使用英雄的方式设置他们像这样
heroku config:add aws_access_key = myapikey
,但是英雄们访问这些像
aws_access_key_id = ENV ['aws_access_key']
如何设置我的开发环境使用方法配置和英雄使用ENV,我看这个错误的方式?或者我的配置方法为我做这个?
任何帮助赞赏
RAKEFILE
require'active_support / core_ext'
require'./config/config.rb'
require'bundler / setup'
Bundler.require(:default)
include MyConfig
AssetSync.configure do | con |
con.fog_provider ='AWS'
con.fog_region ='eu-west-1'
con.fog_directory = config ['fog_directory']
con.aws_access_key_id = config [ 'aws_access_key']
con.aws_secret_access_key = config ['aws_secret_key']
con.prefix =assets
con.public_path =路径名(./ public)
end
命名空间:assets do
desc预编译资产
任务:precompile do
AssetSync.sync
end
end
更新:
我现在使用 dotenv 宝石,而不是下面的示例。因此,我现在忽略了 env.rb
文件,现在我忽略了使用Git的 .env
文件。
原始帖子
尝试这个,
#/env.rb
ENV ['aws_bucket'] ='my_bucket'
ENV ['aws_access_key'] ='my_access_key'
ENV ['aws_access_secret'] ='my_access_secret'
此文件设置相同的 ENV
值为 heroku config
会做。
#/config.rb
require'./env'如果File.exists?(' env.rb')
env.rb
只有在存在的情况下才需要。
#/.gitignore
/env.rb
env.rb
已添加到 .gitignore
文件,因此它不保存在Git中。
然后,您将使用 ENV ['key']
而不是 config ['key']
。
您可能需要将路径更改为 env.rb
如果它不在与 config.rb
文件相同的目录中。
编辑:
从上一个问题中查看您的 Rakefile
,您需要将其更改为:
#Rakefile
要求'bundler / setup'
Bundler.require(:default )
require'./env'如果File.exists?('env.rb')
AssetSync.configure do | con |
con.fog_provider ='AWS'
con.fog_region ='eu-west-1'
con.fog_directory = ENV ['aws_bucket']
con.aws_access_key_id = ENV [ 'aws_access_key']
con.aws_secret_access_key = ENV ['aws_access_secret']
con.prefix =assets
con.public_path =路径名(./ public)
end
命名空间:assets do
desc预编译资产
任务:precompile do
AssetSync.sync
end
end
我假设 /config/config.rb $ c中唯一的方法$ c>是
config
方法,所以我删除了,
require'./config/config.rb'
include MyConfig
并交换 config [key]
for ENV [key]
在 env.rb $中定义的值C $ C>。您可能需要更改
密钥
名称才能匹配。
I have a sinatra app in which i have a yml file to set environment variables, i call them using this method
module MyConfig
def config
environment = ENV["RACK_ENV"] || "development"
YAML.load_file("./config/config.yml")[environment]
end
end
so when i want to use a variable i do this for example
aws_access_key_id = config['aws_access_key']
I have a .gitignore file that ignores config.yml when pushing to github for example.So when I push to heroku these environment variables will not be accessible?
So this leaves me with using the heroku way of setting them like so
heroku config:add aws_access_key= myapikey
but heroku accesses these like
aws_access_key_id = ENV['aws_access_key']
How can i set my dev environment to use method config and heroku use ENV, am i looking at this the wrong way? or does my config method do this for me?
Any help appreciated
RAKEFILE
require 'active_support/core_ext'
require './config/config.rb'
require 'bundler/setup'
Bundler.require(:default)
include MyConfig
AssetSync.configure do |con|
con.fog_provider = 'AWS'
con.fog_region = 'eu-west-1'
con.fog_directory = config['fog_directory']
con.aws_access_key_id = config['aws_access_key']
con.aws_secret_access_key = config['aws_secret_key']
con.prefix = "assets"
con.public_path = Pathname("./public")
end
namespace :assets do
desc "Precompile assets"
task :precompile do
AssetSync.sync
end
end
Update:
I now use the dotenv gem instead of the example below. So instead of ignoring the env.rb
file, I now ignore the .env
file with Git.
Original post:
Try this,
# /env.rb
ENV['aws_bucket'] = 'my_bucket'
ENV['aws_access_key'] = 'my_access_key'
ENV['aws_access_secret'] = 'my_access_secret'
This file sets the same ENV
values as heroku config
would do.
# /config.rb
require './env' if File.exists?('env.rb')
The env.rb
will only get required if it exists.
# /.gitignore
/env.rb
The env.rb
has been added to the .gitignore
file so it isn't kept in Git.
You would then access the values using ENV['key']
instead of config['key']
.
You might need to change the path to the env.rb
if it's not in the same directory as the config.rb
file.
EDIT:
From looking at your Rakefile
in the previous question, you need to change it to this:
# Rakefile
require 'bundler/setup'
Bundler.require(:default)
require './env' if File.exists?('env.rb')
AssetSync.configure do |con|
con.fog_provider = 'AWS'
con.fog_region = 'eu-west-1'
con.fog_directory = ENV['aws_bucket']
con.aws_access_key_id = ENV['aws_access_key']
con.aws_secret_access_key = ENV['aws_access_secret']
con.prefix = "assets"
con.public_path = Pathname("./public")
end
namespace :assets do
desc "Precompile assets"
task :precompile do
AssetSync.sync
end
end
I've assumed that the only method in /config/config.rb
was the config
method so I've removed the,
require './config/config.rb'
include MyConfig
And swapped the config[key]
for the ENV[key]
values defined in env.rb
. You may need to change the key
names to match up.
这篇关于环境变量在本地和Heroku的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!