0

I have a Rails app on a production server and an .rb file where I do this:

Rails.configuration.my_sect = if Rails.env.development? || Rails.env.test?
  {
    secret_key: 'some_secrete',
    public_key: 'some_public'
  }
else
  {
    secret_key: ENV['key1'],
    public_key: ENV['key2']
  }
end

The application is on a Linux server. What's the best place to put the values of those secret_key and public_key on the server so ENV['key1'] and ENV['key2'] can always be accessible?

I don't want to use any gem or Capistrano.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Incerteza
  • 32,326
  • 47
  • 154
  • 261

3 Answers3

1

I would put them in the server script, because, for example, shell configuration files like ~/.bashrc are not loaded in cron scripts or other scenarios.

The "server script" could be as simple as key1=foo key2=baz rails s.

Leonid Shevtsov
  • 14,024
  • 9
  • 51
  • 82
0

Add /config/en_vars.rb to .gitignore

Create a file config/en_vars.rb and put your variables there:

ENV['key1']='foo'
ENV['key2']='bar'

Then in config/environment.rb add the lines below the require line:

en_vars = File.join(Rails.root, 'config/en_vars.rb')
load(en_vars) if File.exists?(en_vars)
jljohnstone
  • 1,202
  • 1
  • 10
  • 14
0

Even if you say you don't want to use any gem, I recommend you to use dotenv; even if it's not suggested to use it on a production environment.

Otherwise, the simplest plain solution is:

  1. create a per-project environment variables file (let's say "$PROJECT_DIR/.env")
  2. Add it to .gitignore
  3. execute it before the application server starting, with something like

    source "$PROJECT_DIR/.env" && rails server
    

But that's what dotenv is created for, so why reinvent the wheel?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
mdesantis
  • 8,257
  • 4
  • 31
  • 63