Everything is working fine on your local machine, so you are making commit and pushing
changes to the origin. Now you need to update server configuration so you’re adding following to
Deploy, and… not working. Environment variables are not available. If you, like me,
always have problems figuring out whether you are in login/interactive shell or not, and
juggling configuration between
.bash_profile perhaps this figure
will help you to understand where settings should go. But you must be aware, that if
you are using solutions like Upstart/Monit to start/stop your application, you can be
confused about which variables and what PATH is available for the application. From the other
hand you still want same variables to be available when you log in via SSH.
But this post is not about how to properly setup environment. It is about fetching environment variables.
The problem I’d like to point out is related to the way
we are reading environment variables from
ENV. In fact you can find that way of
obtaining env variables in Rails 4:
The problem is that if we call
ENV with key that is missing
be returned. That will probably cause weird errors which are far away from the real reason.
It might take some time figuring out that error is caused by misspelled or not configured
environment variable. Reading environment variables via
 does not allow gracefully
handle case when key is missing or set defaults (most probably we’d use
||= to solve that).
There is a better way though.
ENV is respoding to
fetch similiary as
Array are doing. That way
we can set default values or handle - providing a block - gracefully missing
keys. Also using
fetch with unknown key will raise
KeyError that will tell
us which exactly key is missing. That is in fact the behavior we are expecting from the app.
Without required settings is just not working and complaining about missing
setting and not about some random
So refining our previous example, the AWS configuration should look as follows:
The application will tell us which exactly key is missing in case we forgot to set it. Now we can go back figuring out to which file environment variables should go to be available for our application.