You must have come across situations when you have to include sensitive information in your source code, like usernames, passwords, secret keys, etc. If you haven't yet, you most probably will. The problem is, if you want to put your source code in a public repository, say github, you don't want people to learn about that sensitive information, so what would you do?
Here environment variables come into the play and this is how it looks
ENV['MY_SECRET_INFORMATION']
Usually you can set environment variables via terminal, and heroku also allows you to mention all the environment variables your project requires if you deploy your application there, and then there are gems which ease the process for you. I'll be taking a different approach today and not use a specific gem for the task because I like keeping the number of dependencies low.
So we'll create a my_env.yml file in /config/. In that file, say you want to set value for ENV['MY_SECRET_IDENTITY'], you'll have to write
MY_SECRET_IDENTITY: 'Batman'
Pretty simple, isn't it? All you need now is write a simple code for the application to understand that YAML file. Open up your application.rb and add this piece of code
config.assets.version = '1.0'
config.before_configuration do
env_file = File.join(Rails.root, 'config', 'my_env.yml')
YAML.load(File.open(env_file)).each do |key, value|
ENV[key.to_s] = value
end if File.exists?(env_file)
end
Now all your sensitive information can be provided by that single YAML file, just make sure to add this /config/local_env.yml to your .gitignore file, you don't want to check that in, after all you did all this hard work for something!
NOTE: You might also need to restart the server.
Here environment variables come into the play and this is how it looks
ENV['MY_SECRET_INFORMATION']
Usually you can set environment variables via terminal, and heroku also allows you to mention all the environment variables your project requires if you deploy your application there, and then there are gems which ease the process for you. I'll be taking a different approach today and not use a specific gem for the task because I like keeping the number of dependencies low.
So we'll create a my_env.yml file in /config/. In that file, say you want to set value for ENV['MY_SECRET_IDENTITY'], you'll have to write
MY_SECRET_IDENTITY: 'Batman'
Pretty simple, isn't it? All you need now is write a simple code for the application to understand that YAML file. Open up your application.rb and add this piece of code
config.assets.version = '1.0'
config.before_configuration do
env_file = File.join(Rails.root, 'config', 'my_env.yml')
YAML.load(File.open(env_file)).each do |key, value|
ENV[key.to_s] = value
end if File.exists?(env_file)
end
Now all your sensitive information can be provided by that single YAML file, just make sure to add this /config/local_env.yml to your .gitignore file, you don't want to check that in, after all you did all this hard work for something!
NOTE: You might also need to restart the server.
No comments:
Post a Comment