dotenv is a very convenient way to load env vars for a ruby on rails project. It makes easy to put you env vars on a file(.env) and it loads from that file when a rails server starts. I already worked in a lot of projects that use this gem and capistrano as deployment tool. It is really easy to add a task to capistrano to read all your env vars for a file named .env_production and upload this to your production server as a .env file that will be used to read and load all your env vars from dotenv gem when your rails server starts in production.
But when you work in a project with those configurations, it is common to face a problem when you add a new env var to your code and forget to add a value for this env var in production dotenv file used by capistrano. So your project crash after your deploy and when you check the error, all you need to do is to add a value for the new env var used and do a new deploy to fix.
Sometime ago I faced this problem not for the first time, enough to make me want to find a solution. I started to search for a tool that stop the capistrano deploy if there are some env var used in the project code that was not defined and, since I did not find any tool for this, I decided to build a simple gem for this job.
capistrano-dotenv it really simple, it mainly adds three tasks to capistrano, so you can read all your env vars from a file, check if all env vars used on your project code were defined, and setup this in your remote server uploading it in a .env file that will be used from dotenv gem.
To read all env vars, you just need to add invoke 'dotenv:read' to your capistrano deploy tasks. This task will read all your env vars from a .env file by default, but you can set a custom env file.
After this, if you want to ensure that you doing a deploy without forget to define any env var, just add invode 'dotenv:check' , it will search in your entire rails project for any ENV['SOMEVAR'] and ENV.fetch('SOMEVAR') to find all env vars used and then will compare the list found with the variables defined in the env file read before. If there is anyone that this task found in code but was not found in env file, the deploy will fail and output in terminal all the env vars that are missing to be defined.
Because sometimes we add some env variables to use only on development or only in on staging and not in production, and sometimes a env variable is used only locally, it is possible to set a list of optional and a list of ignored env vars. In the case of optional env vars, if some variable from this list was found in the code but not in the env file, the deploy will not fail, but it will ask in the terminal for a confirmation if you want to continue the deploy without define this variable.
So, the big difference between this gem that I built and some other check tools that I found in the github, is that I wanted a solution that I did not have to remember to add the new env var to a list of check variables, I wanted that after I add a new env var on my code, this will be checked automatically, without any need to remember anything. So, I prefer to have to have a deploy failed because I forgot to add a optional env var to the optional list than have my project crashed in production because I forgot to define a required env var and forgot to add this to a required env vars list.
So, the code is open source and is available on my github, any suggestion and contribution is great. Any feedback too.
Top comments (0)