Environment variables locally and Heroku

拥有回忆 提交于 2019-12-04 00:04:26

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.

You could delete the yaml, and describe the environment variables in a .env file then start your app with foreman start. See https://devcenter.heroku.com/articles/config-vars#local-setup


Or keep your hybrid system, where you load a yaml in dev, and use environment variables on heroku.

I do something similar to Sam's suggestion, but a little bit different. I have a YAML config file too, but I wrap the reading of it in a Rake task, which then runs the app.

# in the Rakefile

require 'yaml'

def set_connstring
  s = %Q!postgres://#{ENV["DB_APP"]}@localhost/#{ENV["DB_APP"]}!
  ENV['DATABASE_URL'] ||= ENV["RACK_ENV"] == "test" ? "#{s}.test" : s
end


def basic_environment
  warn "  Setting up environment..."

  file = File.expand_path( File.join File.dirname(__FILE__), "./config.yml" )
  if File.exist? file
    YAML.load_file(file).each do |k,v|
      warn "-> #{k}"
      ENV[k.upcase] = v
    end
  end

  set_connstring()
end

namespace :app do

  desc "Set up the environment locally"
  task :environment do
    basic_environment()
  end

  desc "Run the app locally"
  task :run_local => "app:environment" do
    exec "bin/rackup config.ru -p #{ENV['RUN_LOCAL_PORT']}"
  end
end

It means I can run it locally without any code inside the app to deal with this.


Edit: a quick aside, I notice you have Bundler.require(:default) in your Rakefile. If you use bundle install --binstubs then Bundler installs all executables into a dir named "bin/" within the project. Then, if you run any of those executables they automatically use the libraries installed by Bundler, no need to require via Bundler. See http://gembundler.com/v1.2/man/bundle-exec.1.html.

Tom Lobato

Exporting directly from heroku admin: Settings -> Reveal Config Vars

Then open browser js console, paste this and type enter...

k=[];
$(".config-var-list input").map(function(y, x){k.push($(x).val())});
v=[];
$(".config-var-list textarea").map(function(y, x){v.push($(x).val())});
ret="";
k.map(function(x, i){ret+=k[i]+"\t"+v[2*i]+"\n"});
console.info(ret);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!