I am updating heroku config through my rails application.
Like-
def add_to_heroku_config
Rails.logger.debug "Executing: #{ self.name.capitalize }=#{ self.database_string }"
Bundler.with_clean_env { system("heroku config:set #{ self.name.capitalize }=#{ self.database_string }") }
end
Now this works from local machine, when i deploy it on heroku, obivously it is not going to work.
Now Is it possible to update/add ENV variables from the application running on heroku itself ?
I'm using the Figaro gem for this, allowing you to set the environment variables on Heroku, using a .yml file. Figaro contains a rake task doing exactly what you're asking, without making the environment variables "accessible" on the server, obviously for security reasons, by adding the .yml file to .gitignore
See https://github.com/laserlemon/figaro
Related
Where do I put environment variables in Rails and can I make a file for checking the environment before running?
In a MERN stack app(all javascript), I created a 3 files in a config folder:
keys.js
dev.js
prod.js
keys.js has the following code:
if (process.env.NODE_ENV === 'production') {
module.exports = require('./prod');
} else {
module.exports = require('./dev');
}
dev.js contains all of the variables for google, mongo, stripe, etc.
'prod.js' has the following code:
module.exports = {
googleClientID: process.env.GOOGLE_CLIENT_ID,
googleClientSecret: process.env.GOOGLE_CLIENT_SECRET,
mongoURI: process.env.MONGO_URI,
cookieKey: process.env.COOKIE_KEY,
stripePublishableKey: process.env.STRIPE_PUBLISHABLE_KEY,
stripeSecretKey: process.env.STRIPE_SECRET_KEY,
sendGridKey: process.env.SENDGRID_KEY,
redirectDomain: process.env.REDIRECT_DOMAIN
};
This config folder is in the root of the app structure.
I've done this to deploy to Heroku and it works perfectly. My development env allows me to test the APIs as it would in production. It's been ignored within my repo so the dev.js is not available on Github.
My question: where to I put the such a file in a Rails application? Can I create such a file to store my env variables?
I'd like to use the same logic if possible and I can figure the code out to do so but I'm tepid about where to put the file(s).
I like using the Figaro gem to manage my ENV variables in Rails.
Setup
Add the gem to your Gemfile:
gem 'figaro'
Install the gem by running $ bundle install
Set up Figaro: $ bundle exec figaro install (this creates a file called application.yml under the config/ directory)
Add your ENV variables to the new config/application.yml file
Use
Add your ENV variables to the config/application.yml file. For example,
# config/application.yml
google_client_id: '7381a978f7dd7f9a1117'
google_client_secret: 'abdc3b896a0ffb85d373'
mongo_uri: 'https://www.example.com'
...
You can then use the ENV variables in your Rails code, wherever you need them: ENV['google_client_id']
Different ENV variables and values for different environments...
You can specify different values for your variables for different environments, say development and test. Here is an example from Figaro's README:
# config/application.yml
pusher_app_id: "2954"
pusher_key: "7381a978f7dd7f9a1117"
pusher_secret: "abdc3b896a0ffb85d373"
test:
pusher_app_id: "5112"
pusher_key: "ad69caf9a44dcac1fb28"
pusher_secret: "83ca7aa160fedaf3b350"
Pushing ENV variables to Heroku
It sounds like you're using Heroku. Figaro makes it easy to deploy all your Rails environment variables to Heroku at once: figaro heroku:set -e production
More
There is a lot more you can do with Figaro, just check out their Github Readme.
I added the key into heroku config var, but I'm still getting the error.
Is this the correct way? I ignored secrets.yml as I read from other sources that its not a good idea to push this to the public.
in the heroku config var:
[key] SECRET_KEY_BASE
[value] 3280570382948240938
in secrets.yml
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
What am I still doing wrong?
Furthermore, if I put my secret keys into heroku's config variable, don't other developers get to see this too? So, isn't that still kind of public? I've always wondered this concept.
you can set environment variable with heroku config
first generate secret key with run below command on terminal
rake secret
Now use that key on below command
heroku config:set SECRET_KEY_BASE='put here new generated key'
you can refer this link for more refference
https://devcenter.heroku.com/articles/config-vars
here is a fool-proof way to set the secret key base with heroku:
heroku config:set SECRET_KEY_BASE=$(rake secret)
you can see it with heroku config:get SECRET_KEY_BASE
and check that rails picks it up with Rails.application.secret_key_base (in heroku run rails console for instance)
I had the same issue today, but for me the solution was a bit different. I realized that in my local environment, I had been using:
Rails.application.secrets.secret_key_base
but for Heroku, instead use:
Rails.application.secret_key_base
^This worked in my local environment as well. Not sure what the extra .secrets is for.
I'd like to run my RoR app on Appfog in the "staging" environment rather than the default "production" environment.
I tried to add an environment variable RAILS_ENV=staging, and restarted the app. However, I got this error:
rake aborted!
database configuration does not specify adapter
Does anyone try to do this on Appfog ?
Creating new Environment:
Assuming you want create the hudson environment.
Create a new environment file in config/environments/hudson.rb.
You can start by cloning an existing one, for instance config/environments/test.rb. Add a new configuration block in config/database.yml for your environment. That's all.
Now you can start the server
ruby script/server -e hudson
Run the console
ruby script/server hudson
And so on.
I am a rails developer and trying to get familiar with Heroku.
I don't add config/environment.rb to git as environment.rb might be different for each environment.
For example, ENV['RAILS_ENV'] = 'some_env' should be in environment.rb.
However, Heroku requires environment.rb to be in git so that I can push it to Heroku.
Is there a way to just copy environment.rb to Heroku without version-controlling it?
Thanks.
Sam
You always want to keep the environment.rb file checked in. You can set the environment using config variables
heroku config:add RAILS_ENV=some_env
So you store your config with each individual app. You can read any config variables from your application by using ENV[]
I have writen a ruby script that requires config/environment.rb so I can use all the models of my rails application in the script but the problem is that I want to use the production environment instead of the develoment environment which seems to be the default behavior.
Im using Rails 3.1.1 and Ruby 1.9.2
How can I run the script with the production environment?
Your script will take the environment variable RAILS_ENV as the environment to use.
I'd be very wary of overriding that in the script as it may cause much confusion if you try and run your script in another environment - e.g. staging - and it starts trying to access production databases etc.
So do either:
RAILS_ENV=production ./script/my-awesome-script
or
export RAILS_ENV=production
./script/my-awesome-script
Generally speaking; when I log into a production Rails environment I'd be changing the environment straight away if I haven't configured it to be "production" by default.
I think you want either Rails.env = 'production' or ENV['RAILS_ENV'] = 'production' in your script.
#davidb, I am not sure what u want also not as good on rails yet but may be you can use the seed.rb (i.e seed functionality) for achieving what you want if this script is run only once or sometimes as we can specify the environment while running seed
rake db:seed RAILS_ENV=production