Blog

Figaro gem - Configuration de l'application Rails simple et conviviale pour Heroku à l'aide d'ENV et d'un seul fichier YAML

Icône flèche bleue vers la gauche
Retour au blog
Figaro gem - Configuration de l'application Rails simple et conviviale pour Heroku à l'aide d'ENV et d'un seul fichier YAML

Figaro gem - Configuration de l'application Rails simple et conviviale pour Heroku à l'aide d'ENV et d'un seul fichier YAML

January 19, 2014

Figaro gem

Even if you’re kind enough to offer your hard work to the community (as a just reward for all the pretty gems we use every day), it quickly comes to your mind that some parts of your credentials need to be kept private (even for an open source project). These risks can be avoided by setting ENV variables (key/value) on your local environment and calling them in your code.

config/initializers/credentials.rb

MyApp.app_id = ENV["MY_APP_ID"]
MyApp.key = ENV["MY_APP_KEY"]
MyApp.secret = ENV["MY_APP_SECRET"] |

As an alternative, that could furthermore allow you to set environment specific variables, comes the figaro gem.

Simply add gem "figaro" to your gemfile and run the bundle install command.

Next step is to launch rails generate figaro:install which will create the config/application.yml file and add it into your .gitignore.

You don’t even need to update your initialization file, the gem handle the ENV variable creation and as mentioned earlier in this post, you can now specify some values according to the environment.

config/application.yml

MY_APP_ID: 111111
development:
MY_APP_ID: 222222
staging:
MY_APP_ID: 333333
production:
MY_APP_ID: 444444 |

Let’s now deploy it !

Either with capistrano:

And then you should consider adding something like this into your deploy.rb script:

namespace :deploy do
desc 'Symlink shared directories and files'
task :symlink_directories_and_files do
run "ln -s #{shared_path}/config/application.yml #{release_path}/config/application.yml"
end
end

Or, if you’re using platform like Heroku, the gem includes a nice Rake task which allows you to set all the needed ENV variables:

rake figaro:heroku

For further information:

Prêt à créer votre produit logiciel ? Contactez-nous !