capistrano部署shell脚本在通过github webhook执行时失败

时间:2016-05-30 02:50:52

标签: shell github capistrano webhooks continuous-deployment

我正在尝试使用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

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

1 个答案:

答案 0 :(得分:0)

正在侦听webhook的Rails应用程序已经拥有自己的Bundler环境。当您尝试使用system发送到脚本时,脚本将继承当前的Bundler环境。这可能就是为什么你得到一个&#34; capistrano不是捆绑的一部分&#34;错误。

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

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

来自Bundler&#39; bundle exec documentation

  

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

  

如果要炮轰到另一个捆绑包,也需要使用with_clean_env。在子shell中运行的任何Bundler命令都将继承当前的Gemfile,因此需要在不同bundle的上下文中运行的命令也需要使用with_clean_env。