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

5

37

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!

Sparrowgrass answered 29/9, 2011 at 19:41 Comment(0)
D
57

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.

Diazonium answered 27/9, 2012 at 21:6 Comment(4)
hmmm, not working for me see #16890142Diptych
In Capistrano 3 it's set :default_env, { ... }Hypocorism
Stupid question, but I'm trying the same thing and it's not working. Anyway to debug it?Grommet
How could puts these in a .env file?Hinda
D
10

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

Dhole answered 18/2, 2012 at 18:10 Comment(1)
This might work, but it requires root-privilege settings change. See my answer.Diazonium
A
5

I think you have in fact 2 problems:

1) You want to change the PATH on your remote host(s).

Alter/set the path in your .bashrc on your remote host(s) and run cap> printenv, if your path is right, goto #2, else try to add export BASH_ENV=~/.bashrc to your /etc/profile (be careful, ~/.bashrc will then be run for all non-interactive shell for all users)

2) You want sudo to keep your PATH

Run visudo on your remote host(s) and add:

Defaults        exempt_group = "<your_user>"
Auraaural answered 7/10, 2011 at 9:17 Comment(1)
Good call - I wasn't aware that sudo obscured the local environment. Looks like just adding the -E option fixes most cases, but $PATH is special (reference). I'll play around with this a bit more and get back to you. Cheers!Sparrowgrass
F
4

I needed to set an environment variable for a specific task to work. The "run" command allows you to pass options which include :env:

run "cmd", :env => { 'name' => 'value' }

In my case, I wanted to add the environment variable to a task that I didn't write, so I used default_run_options which is used by all invocations of run. I added this to the top of my Capfile:

default_run_options[:env] = { 'name' => 'value' }
Funnelform answered 19/9, 2012 at 20:55 Comment(2)
I've seen this solution elsewhere, but I can't get it to work for me - I get a NameError: undefined local variable or method default_run_options' for main:Object` error. I put that assignment statement at the top of my Capfile, before the first namespace :deploy do block. What could I be doing differently?Nonagon
How are you running the task? If you run cap task, it should define default_run_options during the load process so it would be available by the time it gets to your Capfile. Even with a one-line Capfile that just says default_run_options[:x] = { 'y' => 'z' }, I can run cap shell.Funnelform
N
0

I tried unsuccessfully to use @brian-deterling's technique, which is pretty commonly used by others who have discussed this... Maybe I'm doing something wrong, but meanwhile I found the dotenv-rails gem, and it worked very nicely for loading up values out of a .env file in my project root.

The instructions on their Github repo are pretty straight-forward. I added the Dotenv.load to my config/application.rb

Nonagon answered 10/4, 2014 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.