2016-05-30 112 views
1

我試圖使用github webhooks和capistrano實現持續部署例程。當通過github webhook執行capistrano部署shell腳本失敗

我的計劃是將我的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中手動執行它

[email protected]:~/apps/ci/current$ ./wallet_deploy.sh 

不知道我在做什麼錯,是否有不同的方法來實現這個?

回答

0

您正在監聽webhook的Rails應用程序已經擁有自己的Bundler環境。當您嘗試使用system來詮釋您的腳本時,該腳本會繼承當前的Bundler環境。這可能是你爲什麼得到「capistrano不是捆綁包」的錯誤。

爲了確保新鮮捆紮機的環境中使用您的腳本,試試這個:

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

從捆紮機的bundle execdocumentation

打開一個子shell(如系統中的任何Ruby代碼,反引號,或%x {})將自動使用當前的Bundler環境。如果您需要對不屬於當前包的Ruby命令執行shell命令,請在塊中使用with_clean_env方法。

和:

使用with_clean_env也是必要的,如果你是炮擊了一個不同的包。任何在子shell中運行的Bundler命令都將繼承當前的Gemfile,因此需要在不同bundle的上下文中運行的命令也需要使用with_clean_env。

+0

謝謝!它的工作 –

+0

@SharnJayantha你可以標記答案爲接受,然後呢?謝謝 –