Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've got a staging server with both standard Ruby and Ruby Enterprise installed. As standard Ruby refuses to install a critical gem, I need to set $PATH so that ruby/gem/rake/etc. always refer to the REE versions. And since I use Capistrano to deploy to our machines, I need to do it in Capistrano.

How can I set an environment variable once, and have it persist throughout the Capistrano session?

1) It's easy to do in bashrc files, but Capistrano doesn't read bashrc files.

2) I'd use Capistrano's

default_environment['PATH'] = 'Whatever' 

but Capistrano uses these environment variables like

env PATH=Whatever command arg ... 

and they're lost whenever another shell is spun up within the executable passed to env. Like when you use sudo. Which is kinda important:

[holt@Michaela trunk]$ env VAR=hello ruby -e "puts ENV['VAR']" hello [holt@Michaela trunk]$ env VAR=hello sudo ruby -e "puts ENV['VAR']" nil 

3) And I can't use the bash export command, as these are lost too - Capistrano seems to start up a new shell for each command (or something like that), and that's lost, too:

cap> export MYVAR=12 [establishing connection(s) to xxx.xxx.xxx.xxx] cap> echo $MYVAR  ** [out :: xxx.xxx.xxx.xxx]  cap>  

4) I've tried messing with Capistrano's :shell and :pty options as well (and in combination with the other approaches), but no luck there, either.

So - what's the right way to do this? This seems like such a basic task that there should be a really simple way to accomplish it, but I'm out of ideas. Anyone?

Thanks in advance!

like image 600
Xavier Holt Avatar asked Sep 29 '11 19:09

Xavier Holt


People also ask

Should env variables be all caps?

By convention, environment variables are written in UPPERCASE while shell variables usually have lowercase names.

How do I set environment variables permanently?

You can set an environment variable permanently by placing an export command in your Bash shell's startup script " ~/. bashrc " (or "~/. bash_profile ", or " ~/. profile ") of your home directory; or " /etc/profile " for system-wide operations.

How do I set an environment variable in Korn shell?

To set an environment variable in sh or ksh, use the syntax VAR=value;export VAR, where VAR is the name of the environment variable and value is the value you wish to assign.


2 Answers

I have the exactly same problem, but I think this solution is better:

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

This works for me like a charm.

like image 139
Peter Lee Avatar answered Oct 15 '22 02:10

Peter Lee


If you need to set a variable on the remote host other than PATH, you should know that sshd only allows certain /etc/profile or ~/.bashrc environment variables by default, for security reasons. As Lou said, you can either do cap shell and use the cap> printenv command, or you can do cap COMMAND=printenv invoke in one command.

If you see the variable when you ssh into the remote shell normally, but you don't see it in the cap printenv command, here's one solution:

  1. Set PermitUserEnvironment yes in your remote server's /etc/ssh/sshd_config file, and restart sshd
  2. Edit the ~/.ssh/environment file for the remote user you are ssh'ing in as, and put your variable(s) there as VARIABLE=value

Now those should show up when you do cap COMMAND=printenv invoke

like image 22
bjnord Avatar answered Oct 15 '22 00:10

bjnord