Capistrano and environment variables Capistrano and environment variables ruby-on-rails ruby-on-rails

Capistrano and environment variables


You might be best looking at the difference between ENVIRONMENT VARIABLES and SHELL VARIABLES

When you fire SSH, your app will load the SHELL variables which are defined in your .bashrc file. These only exist for the life of the shell, and therefore, we don't use them as much as ENV vars

You may be better putting the ENV vars in:

/etc/environment

Like this:

export ENVIRONMENT_VAR=value

This will make the variables available throughout the system, not just in different shell sessions


Update

Have you tried

Capistrano: Can I set an environment variable for the whole cap session?

set :default_env, {   'env_var1' => 'value1',  'env_var2' => 'value2'}


Although this has been answered, I'm going to leave this here in case anyone else is in the same situation I was.

Capistrano does load .bashrc. But if you'll notice at the top of the file there is this:

# If not running interactively, don't do anything[ -z "$PS1" ] && return

The solution was simply to put any setup above this and Capistrano works how I want.

This solution was also noted at this GitHub issue.


In order to debug the issue update config/deploy.rb with a simple task:

namespace :debug do  desc 'Print ENV variables'  task :env do    on roles(:app), in: :sequence, wait: 5 do      execute :printenv    end  endend

now run cap staging debug:env. You should be able to see effective configuration of ENV variables.

The order and names of files are dependent on your distribution, e.g. on Ubuntu the sourcing sequence is following:

  1. /etc/environment
  2. /etc/default/locale
  3. /etc/bash.bashrc
  4. ~/.bashrc

When ~/.bashrc contains first lines like this, any code afterwards won't be sourced:

# If not running interactively, don't do anythingcase $- in    *i*) ;;      *) return;;esac

To understand how capistrano loads ENV variables this chart (source) might be helpful.

Most likely the ~/.bash* file is not loaded due to non-interactive session.

capistrano env variables loading