title: Rails 4, Capistrano 3 and Dotenv - How to deploy using server-side .env file
body: <p>I have a Rails 4 app with Dotenv gem to read variables from the file <code>.env</code>.</p>
  
  <p>There are some variables I've set in order to have a mysql user other than "root" for my rails app, for example:</p>
  
  <pre><code>MYSQL_ROOT_USER='rootuser'
  MYSQL_ROOT_PASSWORD='rootpassword'
  APP_DATABASE_USER='mydbuser'
  APP_DATABASE_PASSWORD='userpassword'
  </code></pre>
  
  <p>I've also created a bash script to create the mysql user under <code>scripts/database_setup.bash</code></p>
  
  <pre><code>#!/bin/bash
  source ../.env
  # creates the user
  mysql -u${MYSQL_ROOT_USER} --password="${MYSQL_ROOT_PASSWORD}" -e "CREATE USER '${APP_DATABASE_USER}'@'localhost' IDENTIFIED BY '${APP_DATABASE_PASSWORD}';"
  # grants permission
  mysql -u${MYSQL_ROOT_USER} --password="${MYSQL_ROOT_PASSWORD}" -e "GRANT ALL PRIVILEGES ON \`myapp\_%\`.* TO '${APP_DATABASE_USER}'@'localhost';"
  </code></pre>
  
  <p>On the server side, Capistrano deploys to `/home/myuser/apps/myapp/</p>
  
  <p>I have three questions:</p>
  
  <ol>
  <li>Where is the best place to put my server-side <code>.env</code> file? Right now I'm putting it in <code>/home/myuser/apps/myapp/</code> directory.</li>
  <li>How can I tell Capistrano to copy it to Rails root directory?</li>
  <li>How can I tell Capistrano to execute my bash script before running migrations?</li>
  </ol>