Rails doesn't see environment variable
Asked Answered
L

6

11

I am trying to move a Rails application into production but I am having a problem with Rails not seeing my environment variable.

I have the password for my database setup in my .bashrc file like

export APP_NAME_DATABASE_PASSWORD=secretkey

In irb

ENV["APP_NAME_DATABASE_PASSWORD"]

returns secretkey.

Using

RAILS_ENV=production rails c

and just

rails c

returns secretkey but when starting the application I get

Access is denied (using password: NO)

I am using a slightly modified version of the init script on "How To Deploy a Rails App with Unicorn and Nginx on Ubuntu 14.04" to start unicorn.

It is being hosted on Ubuntu Server 14.04.

Literal answered 8/4, 2016 at 20:51 Comment(3)
Where are you deploying it? Heroku? GoogleCloud? AWS?Margravine
To my own server using Ubuntu 14.04, i'll add that.Literal
LPT: you can look into environment variables of your process by looking into /proc/PID/environ. It's quite helpful for debugging this sort of things.Johnsen
R
9

export APP_NAME_DATABASE_PASSWORD=secretkey is most likely scoping your environment variable to a bash process. Since Unicorn doesn't run as a child of bash process, it doesn't have access to this environment variable.

I'd recommend storing your ENV vars in a single place, such as application.yml and loading them into your ruby environment at the start of your application. There are some great tools that do this. I'd recommend looking into Figaro: https://github.com/laserlemon/figaro.

Here's another post relevant to your question: Re-source .bashrc when restarting unicorn?

Rawinsonde answered 8/4, 2016 at 21:4 Comment(4)
Is there any way to put those variables in Unicorns scope? Or is that a bad Idea?Literal
Yes, you can set them directly in the script that starts unicorn, or in the unicorn.conf file. However, one disadvantage of this approach, however, is that your ENV variables won't be accessible for rake and the rails console.Rawinsonde
I think Anthony explanation is probably right. But i think storing ENV vars in an yml file is not good. If you do that sensitive information could be committed to SCM, which could be an opening to vulnerabilities. Please take a look at dotenv gem i think it is what youre looking for.Unspotted
Yeah don't commit application.yml! same goes for dotenv.Rawinsonde
S
24

Try doing spring stop followed by rails c

Spring is a rails application preloader that loads the ENV configuration. It might not have loaded the .bashrc changes in your case.

Sarcoid answered 8/5, 2019 at 7:5 Comment(0)
P
13

Or you forgot to install gem 'dotenv-rails' ¯_(ツ)_/¯

Pro answered 4/3, 2021 at 1:35 Comment(0)
R
9

export APP_NAME_DATABASE_PASSWORD=secretkey is most likely scoping your environment variable to a bash process. Since Unicorn doesn't run as a child of bash process, it doesn't have access to this environment variable.

I'd recommend storing your ENV vars in a single place, such as application.yml and loading them into your ruby environment at the start of your application. There are some great tools that do this. I'd recommend looking into Figaro: https://github.com/laserlemon/figaro.

Here's another post relevant to your question: Re-source .bashrc when restarting unicorn?

Rawinsonde answered 8/4, 2016 at 21:4 Comment(4)
Is there any way to put those variables in Unicorns scope? Or is that a bad Idea?Literal
Yes, you can set them directly in the script that starts unicorn, or in the unicorn.conf file. However, one disadvantage of this approach, however, is that your ENV variables won't be accessible for rake and the rails console.Rawinsonde
I think Anthony explanation is probably right. But i think storing ENV vars in an yml file is not good. If you do that sensitive information could be committed to SCM, which could be an opening to vulnerabilities. Please take a look at dotenv gem i think it is what youre looking for.Unspotted
Yeah don't commit application.yml! same goes for dotenv.Rawinsonde
H
4

I had a similar problem. A simple solution for you would be to run:

export RAILS_ENV=production && rails c

The reason is that when you do not use the export, the rails can not see the environment variable but you see its value when you execute echo on the console.

Higinbotham answered 19/7, 2018 at 23:40 Comment(0)
A
2

Here is another option:

http://railsguides.net/how-to-define-environment-variables-in-rails/

Put your environment variables in local_env.yml, and then put this in application.rb

  config.before_configuration do
    env_file = File.join(Rails.root, 'config', 'local_env.yml')
    YAML.load(File.open(env_file)).each do |key, value|
      ENV[key.to_s] = value.to_s
    end if File.exists?(env_file)
  end
Acherman answered 8/4, 2016 at 21:58 Comment(0)
D
2

For the ones using zsh:

echo 'export APP_NAME_DATABASE_PASSWORD=value' >> ~/.zshenv
source ~/.zshenv

Check if it has really been set using:

echo $APP_NAME_DATABASE_PASSWORD # value

For running rails applications you might also want to do spring stop.

Dutyfree answered 14/10, 2020 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.