如何指示Capistrano 3加载我在远程主机上设置的shell环境变量?

我想指示Capistrano加载在远程服务器上定义的环境变量。 我怎样才能做到这一点?

似乎当我在.bashrc文件中导出我的环境变量时,Capistrano不会考虑它们。 Capistrano似乎正在执行/usr/bin/env来创建执行远程命令的环境,但这似乎并没有从.bashrc加载环境变量。

我也告诉你,我也在使用rvm-capistrano (以防它可能有所帮助)。

任何线索?

虽然这个问题已经超过六个月了,但我会留在这里以防万一有人面临同样的问题。

Capistrano实际上加载了.bashrc 。 但是在文件顶部附近你会发现:

 # If not running interactively, don't do anything [ -z "$PS1" ] && return 

如果您在该行之后进行任何export ,则Capistrano将无法访问。 解决方案只是将我的设置置于此之上,而Capistrano就是我想要的。

在这个GitHub问题上也注意到了这个解决方案。

Capistrano不加载.bashrc因为它不是交互式shell。 据我记得虽然它确实加载了.bash_profile但是你可能会有更好的运气使用它。

您可以通过发出以下命令将当前环境变量传递给使用ssh的远程执行:

 env | ssh user@host remote_program 

也从这里举了例子

 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 

看起来你可以使用set环境变量来执行。 因此,请阅读当前的环境变量并使用with设置。

在Capistrano 3中set :default_env, { ... }

像这儿:

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

你可以参考这个: 上一篇文章..