How do you keep secrets.yml secret in rails?

后端 未结 4 531
猫巷女王i
猫巷女王i 2021-02-03 12:28

I\'m pretty new to rails, but I have some experience programming in PHP and other languages. I really like rails, and I\'m working on an application for my company, but I still

4条回答
  •  广开言路
    2021-02-03 12:44

    Here's a (hopefully simple) step by step guide FOR HEROKU that should be performed prior to pushing files (secrets.yml) to GitHub, or another host.

    *I am not an expert on this topic but this worked well for me and seems like a good solution. It combines info from answers to this question as well as answers to this question (Step by Step explanation for using Rails secrets.yml without exposing keys to public repo when deploying to Heroku) to provide a simple guide :)

    1) Copy secrets.yml to another file named secrets_backup.yml

    you should now have two files with the same content as secrets.yml

    2) Add secrets_backup.yml to your .gitignore

    3) Change the text in secrets.yml to the following

    development:
      secret_key_base: <%= ENV["SECRET_KEY_BASE_DEV"] %>
    test:
      secret_key_base: <%= ENV["SECRET_KEY_BASE_TEST"] %>
    production:
      secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
    

    4) cd to your rails project folder on the command line

    5) In the terminal type heroku config:set SECRET_KEY_BASE_TEST=, where should be copied and pasted from the test: secret_key_base: which is in secrets_backup.yml

    6) In the terminal type heroku config:set SECRET_KEY_BASE_DEV=, where should be copied and pasted from the development: secret_key_base: which is in secrets_backup.yml

    7) My secrets.yml file already had the SECRET_KEY_BASE instead of the actual key, so I suspect yours will too. But if not, set the SECRET_KEY_BASE variable as the other two were set above.

    8) Push your repo to GitHub and Heroku

    9) Smile because you're the G.O.A.T and show off your sweet website!

提交回复
热议问题