17

I want to instruct Capistrano to load environment variables that are defined on remote server. How can I do that?

It seems that when I export my environment variables inside .bashrc file, they are not taken into account by Capistrano. Capistrano seems to be executing a /usr/bin/env to create the environment for executing remote commands, but this does not seem to be loading the environment variables from .bashrc.

Let me tell you also that I am using rvm-capistrano too (just in case it might help).

Any clue?

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
p.matsinopoulos
  • 7,655
  • 6
  • 44
  • 92

4 Answers4

42

Capistrano actually does load .bashrc. But near the top of the file you will find one of the following lines:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# If not running interactively, don't do anything
[[ $- != *i* ]] && return
# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

If you do any exporting after the above lines, it will not be reached by Capistrano. The solution was simply to move my setup above this line—and Capistrano works how I want.

This solution was also noted at this GitHub issue.

3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
Delameko
  • 2,544
  • 21
  • 19
0

You can pass your current environment variables to a remote execution with ssh by issuing:

env | ssh user@host remote_program

Also taken the example from here

on roles(:app), in: :sequence, wait: 5 do
  within "/opt/sites/example.com" do
    # commands in this block execute in the
    # directory: /opt/sites/example.com
    as :deploy  do
      # commands in this block execute as the "deploy" user.
      with rails_env: :production do
        # commands in this block execute with the environment
        # variable RAILS_ENV=production
        rake   "assets:precompile"
        runner "S3::Sync.notify"
      end
    end
  end
end

looks like you can use with set environment variables for your execution. So read your current environment variables and set them using with .

Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115
  • But this will use the environment variables on the client machine, the one I am running deploy from. Not the environment variables defined on the remote machine, the one I am deploying to. My questions says: "...to load environment variables that are defined on remote server..." – p.matsinopoulos Aug 26 '14 at 16:43
-1

Capistrano doesn't load .bashrc since it's not interactive shell. As far as I remember though it does load .bash_profile though so you will probably have better luck using that.

  • 5
    I just tried on Ubuntu Server 14.04.1, and .bash_profile was not loaded during a Capistrano-initiated SSH command execution (neither was .profile and .bashrc). Also tried by manually running SSH from command line. – odigity Jan 17 '15 at 05:41
  • 3
    For me it reads `.bashrc` and does not read `.bash_profile`. I'm using CentOS. – Mirko Apr 06 '15 at 15:03
  • Wrong, loads .bashrc on Debian too. – arnaudoff Apr 05 '16 at 09:22
-1

In Capistrano 3 it's set :default_env, { ... }

Like here:

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

You can refer to this: Previous post..

Community
  • 1
  • 1
Jay
  • 94
  • 7
  • 1
    This solution does not answer my question. It loads some environment variables as defined in the hash given. So, it is going to define the environment variable 'env_var1' with value 'value1'. But this is not what I am asking. I want capistrano to run the shell scripts using the environment variables that are already defined in my system for the user that is used to ssh the remote host. – p.matsinopoulos Aug 25 '14 at 15:12
  • @p.matsinopoulos Did you find a solution to this? I am struggling with the exact same issue. – Mike A Jul 19 '16 at 17:53