There are a few ways that work well with Capistrano, in my experience.
rbenv-vars
If you use Ruby via Rbenv on your server, then you are in luck. There is a Rbenv plugin called rbenv-vars that automatically injects environment variables into any Ruby process, which would include your Rails app. Just add your variables to ~/.rbenv/vars
on the server using KEY=value
syntax. That's it.
dotenv
The dotenv gem is a similar solution, but it works as a gem you add to your Rails app and doesn't require Rbenv or any other supporting tools. Add dotenv-rails
to your Gemfile and deploy. Dotenv will automatically look for a .env.production
file in the root of your Rails app. For Capistrano, create a .env.production
file on the server inside Capistrano's shared
directory, and then add .env.production
to :linked_files
. Now every deploy will link to it. Declare your variables using KEY=value
syntax.
.bashrc
Declare your variables with export KEY=value
syntax at very top of the ~/.bashrc
file on the server. On Ubuntu, this file is evaluated even during an non-interactive SSH session. Just make sure you place the declarations on the top, before this case statement:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
CentOS may be a different story, so YMMV.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…