Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment variables locally and Heroku

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
like image 267
Richlewis Avatar asked Mar 17 '13 21:03

Richlewis


People also ask

Are Heroku config vars environment variables?

You need to set the environment variables for your application on Heroku. There are two ways to do it, and in this article, you can see both! The environment variables are called config variables (config vars) on Heroku. As a result, the terms will be used interchangeably in the article.

Does .ENV work in Heroku?

env file on Heroku isn't a good approach. Instead, you can use its built-in support for environment variables, using heroku config:set <var> <value> or its web UI. Either way, you'll get a regular environment variable. But for testing these config vars in local, we need to set up a .

How can I see Heroku environment variables?

Accessing config var values from code js you can access your app's DATABASE_URL config var with process. env. DATABASE_URL .

Can Heroku use localhost?

Start your app locally using the heroku local command, which is installed as part of the Heroku CLI. Your app should now be running on http://localhost:5000/.


1 Answers

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.

like image 182
Sam Avatar answered Sep 21 '22 12:09

Sam