【问题标题】:How can I instruct Capistrano 3 to load my shell environment variables set at remote host?如何指示 Capistrano 3 加载我在远程主机上设置的 shell 环境变量?
【发布时间】:2014-10-18 05:11:23
【问题描述】:

我想指示 Capistrano 加载在远程服务器上定义的环境变量。我该怎么做?

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

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

有什么线索吗?

【问题讨论】:

    标签: ruby capistrano3 rvm-capistrano


    【解决方案1】:

    Capistrano 实际上确实加载了.bashrc。但在文件顶部附近,您会发现以下行之一:

    # 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
    

    如果您在上述行之后执行任何exporting,Capistrano 将无法访问它。解决方案就是将我的设置移到这条线之上——Capistrano 会按照我的意愿工作。

    this GitHub issue 也提到了此解决方案。

    【讨论】:

      【解决方案2】:

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

      env | ssh user@host remote_program
      

      也取自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
      

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

      【讨论】:

      • 但这将使用客户端机器上的环境变量,我正在运行部署的机器。不是在远程机器上定义的环境变量,我正在部署的那个。我的问题是:“...加载远程服务器上定义的环境变量...”
      【解决方案3】:

      Capistrano 不加载 .bashrc,因为它不是交互式 shell。据我所知,虽然它确实加载了.bash_profile,但使用它可能会更好。

      【讨论】:

      • 我刚刚在 Ubuntu Server 14.04.1 上尝试过,在 Capistrano 启动的 SSH 命令执行期间没有加载 .bash_profile(.profile 和 .bashrc 都没有)。也尝试通过从命令行手动运行 SSH。
      • 对我来说,它是 .bashrc,而不是 .bash_profile。我正在使用 CentOS。
      • 错误,在 Debian 上也加载 .bashrc。
      【解决方案4】:

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

      喜欢这里:

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

      你可以参考这个: Previous post..

      【讨论】:

      • 这个解决方案没有回答我的问题。它加载给定哈希中定义的一些环境变量。因此,它将使用值“value1”定义环境变量“env_var1”。但这不是我要问的。我希望 capistrano 使用已在我的系统中为用于 ssh 远程主机的用户定义的环境变量运行 shell 脚本。
      • @p.matsinopoulos 您找到解决方案了吗?我正在努力解决完全相同的问题。
      猜你喜欢
      • 2016-07-15
      • 1970-01-01
      • 2014-07-12
      • 1970-01-01
      • 1970-01-01
      • 2011-12-12
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多