How to use secrets.yml for API_KEYS in Rails 4.1?

前端 未结 4 1324
挽巷
挽巷 2021-02-05 02:55

In one of my recent projects I started out by .gitignoring the files containing secrets and environment variables. So the entire project is committed to the repo ex

4条回答
  •  既然无缘
    2021-02-05 03:05

    One way to do it is to store those secret keys in environment variables. How to set an environment variable is different depending on what operating system you're on. For a linux machine, usually you're editing a .bashrc or .bash_profile file in your home directory and adding a line that looks like:

    export API_KEYS=apikeygoeshere
    

    You'll have to edit the file for whatever user will run rails.

    Then in production.rb, you can refer to those environment variables as:

    ENV["API_KEYS"]
    

    Another option is to use a ruby gem that essentially takes care of that for you, like figaro. The way it works is that you create another file that you don't check in and figaro takes care of setting them up as environment variables, which you can then refer to in your development.rb/production.rb scripts using the ENV["API_KEYS"] above. Because you aren't checking in the file that has all of the environment variables, you'll have to find some way to get that file onto whatever machines are running the code.

提交回复
热议问题