How can I instruct Capistrano 3 to load my shell environment variables set at remote host?
Asked Answered
N

4

18

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?

Nabokov answered 25/8, 2014 at 5:8 Comment(0)
P
45

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.

Psychic answered 30/3, 2015 at 11:4 Comment(0)
S
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 .

Sukhum answered 26/8, 2014 at 14:20 Comment(1)
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..."Nabokov
S
-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.

Supportable answered 25/8, 2014 at 7:59 Comment(3)
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.Carburize
For me it reads .bashrc and does not read .bash_profile. I'm using CentOS.Boyish
Wrong, loads .bashrc on Debian too.Monotonous
B
-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..

Brother answered 25/8, 2014 at 14:31 Comment(2)
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.Nabokov
@Nabokov Did you find a solution to this? I am struggling with the exact same issue.Lorrinelorry

© 2022 - 2024 — McMap. All rights reserved.