【问题标题】:capistrano deployment shell script fails when executed through github webhook通过 github webhook 执行 capistrano 部署 shell 脚本失败
【发布时间】:2016-05-30 02:50:51
【问题描述】:

我正在尝试使用 github webhooks 和 capistrano 实现持续部署例程。

我的计划是将我的 capistrano rake 任务放在一个 shell 脚本中,并从另一个 rails 项目(即 github webhook)的控制器操作中调用它。

这里是 shell 脚本 (wallet_deploy.sh)

#!/bin/bash
cd $HOME/work/wallet
bundle exec cap production deploy > wallet_deploy_log 2>&1

这是日志

/home/deploy/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/bundler-1.11.2/lib/bundler/rubygems_integration.rb:304:in `block in replace_gem': capistrano is not part of the bundle. Add it to Gemfile. (Gem::LoadError)
    from /home/deploy/.rbenv/versions/2.2.4/bin/cap:22:in `<main>'

这里是控制器动作

def release_request
  system("./wallet_deploy.sh")
  #DeployWorker.perform_async // tried using a worker too with no success

  render :text => params.to_s
end

当我在 shell 中手动执行 Cap 部署时,它可以完美运行

deploy@ubuntu14-public:~/apps/ci/current$ ./wallet_deploy.sh

不确定我做错了什么,是否有不同的方法来实现这一点?

【问题讨论】:

    标签: shell github capistrano webhooks continuous-deployment


    【解决方案1】:

    监听 webhook 的 Rails 应用已经拥有自己的 Bundler 环境。当您尝试使用system 对您的脚本进行外壳处理时,该脚本会继承当前的 Bundler 环境。这可能就是您收到“capistrano is not part of the bundle”错误的原因。

    为确保您的脚本使用全新的 Bundler 环境,请尝试以下操作:

    Bundler.with_clean_env do
      system("./wallet_deploy.sh")
    end
    

    来自 Bundler 的 bundle exec documentation

    任何打开子 shell(如系统、反引号或 %x{})的 Ruby 代码都将自动使用当前的 Bundler 环境。如果您需要使用不属于当前捆绑包的 Ruby 命令,请使用带有块的 with_clean_env 方法。

    还有:

    如果您要使用不同的捆绑包,也需要使用 with_clean_env。在子 shell 中运行的任何 Bundler 命令都会继承当前的 Gemfile,因此需要在不同包的上下文中运行的命令也需要使用 with_clean_env。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-06
      • 2020-05-31
      相关资源
      最近更新 更多