Capistrano:我可以为整个 cap 会话设置一个环境变量吗? [英] Capistrano: Can I set an environment variable for the whole cap session?
问题描述
我有一个安装了标准 Ruby 和 Ruby Enterprise 的登台服务器.由于标准 Ruby 拒绝安装关键 gem,我需要设置 $PATH 以便 ruby/gem/rake/etc.始终参考 REE 版本.由于我使用 Capistrano 部署到我们的机器上,所以我需要在 Capistrano 中进行.
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.
我如何设置一次环境变量,并让它在 Capistrano 会话中持续存在?
1) 在 bashrc 文件中很容易做到,但 Capistrano 不读取 bashrc 文件.
1) It's easy to do in bashrc files, but Capistrano doesn't read bashrc files.
2) 我会使用 Capistrano 的
2) I'd use Capistrano's
default_environment['PATH'] = 'Whatever'
但 Capistrano 使用这些环境变量,例如
but Capistrano uses these environment variables like
env PATH=Whatever command arg ...
并且每当在传递给 env 的可执行文件中启动另一个 shell 时,它们就会丢失.就像你使用 sudo 时一样.哪一点很重要:
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) 我不能使用 bash 导出命令,因为这些命令也丢失了 - Capistrano 似乎为每个命令(或类似的命令)启动了一个新的 shell,这也丢失了:
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) 我也尝试过使用 Capistrano 的 :shell 和 :pty 选项(并与其他方法结合使用),但也没有运气.
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?
提前致谢!
推荐答案
我也有同样的问题,但我觉得这个解决方案更好:
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.
这篇关于Capistrano:我可以为整个 cap 会话设置一个环境变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!