2010-09-17 133 views
52

我有一個基本的Rails 3應用程序,在我的開發框本地工作,但想要測試出早期部署以確保一切正常。我正在使用Capistrano進行部署。Rails 3 - Bundler/Capistrano錯誤

當我運行cap deploy(畢竟其他必要的設置),它打破與此錯誤以下命令:

[...] 
* executing 'bundle:install' 
* executing "bundle install --gemfile /var/www/trex/releases/20100917172521/Gemfile --path /var/www/trex/shared/bundle --deployment --quiet --without development test" 

servers: ["www.[my domain].com"] 
[www.[my domain].com] executing command 
** [out :: www.[my domain].com] sh: bundle: command not found 
command finished 
[...] 

所以它看起來像它無法找到服務器上的bundle命令。

然而,當我登錄到服務器...

$ ruby -v 
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux] 
$ rails -v 
Rails 3.0.0 
$ bundle -v 
Bundler version 1.0.0 

...的bundle命令的作品就好了。

可能會出現什麼問題?

-

(此外,完整性:)

$ which ruby 
~/.rvm/rubies/ruby-1.9.2-p0/bin/ruby 
$ which rails 
~/.rvm/gems/ruby-1.9.2-p0/bin/rails 
$ which bundle 
~/.rvm/gems/ruby-1.9.2-p0/bin/bundle 

回答

89

UPDATE:

對於RVM> = 1.11.3,你現在應該只使用rvm-capistrano gem。對於較舊的RVM> = 1.0.1,下面的答案仍然適用。


原來的答案:

好吧,雖然我還是沒有得到一個完整的cap deploy工作,我做了修復這個問題。問題是Capistrano試圖爲Bundler(和其他寶石)使用不同的路徑而不是RVM路徑。

通過做cap shell,然後echo $PATH檢查您的Capistrano路徑。你可能會看到你的標準/usr/local/bin/usr/bin,但這不是存儲Bundler等存儲的RVM的地方。

編輯您的Capistrano的config/deploy.rb文件,並添加以下行,每these instructions

# Add RVM's lib directory to the load path. 
$:.unshift(File.expand_path('./lib', ENV['rvm_path'])) 

# Load RVM's capistrano plugin.  
require "rvm/capistrano" 

set :rvm_ruby_string, '1.9.2' 
set :rvm_type, :user # Don't use system-wide RVM 

終於得到了Capistrano的看到捆紮機,並適當地加載啓動的寶石。

+1

我有RVM安裝系統範圍,並與本地用戶部署。爲了使它工作,我必須確保我正在部署的用戶已經設置了rvm的權利。在http://rvm.beginrescueend.com/rvm/install/這裏描述的.bashrc疑難解答幫助 – 2011-01-31 19:01:31

+1

也許你會發現自己在這裏,如果你嘗試從這裏列出的rvm capistrano集成升級:http:// ariejan。 net/2011/09/14/lighting-fast-zero-downtime-deployments-with-git-capistrano-nginx-and-unicorn?utm_source = ruby​​weekly&utm_medium =發送電子郵件至此答案中提到的新方法。如果你刪除'default_environment'行,一定要刪除'default_run_options [:shell] ='bash'',否則你仍然不會在capistrano shell上擁有rvm。 – 2011-09-28 01:53:23

+0

這對我不起作用。只有這樣:set:bundle_cmd,'source $ HOME/.bash_profile && bundle' – hipertracker 2011-11-27 03:41:14

7

最後一行實際上應該是

set :rvm_type, :user 

也就是說,用戶必須是一個符號,而不是一個變量,否則你會得到

undefined local variable or method `user' 
+1

你是對的。答案已更新。 – 2011-02-10 19:56:42

2

這是我的理解是,束命令沒有找到,因爲在用戶的〜/ .bash_profile中定義的PATH變量未被Capistrano加載。

爲了解決這個問題,我創建了一個任務:bundle_gems。

task :bundle_gems do 
    run "cd #{deploy_to}/current && export PATH=/usr/local/pgsql/bin:/opt/ruby-enterprise-X.X.X/bin:$PATH && bundle install vendor/gems" 
end 

請注意,我還包括路徑PostgreSQL的二進制文件 - 安裝PG寶石是失敗,因爲他們無法找到,即使束可以找到。

雖然這看起來很亂。據推測,還有一個更「全球」的地方來定義我不知道的二進制文件路徑。

更新23/12

要添加目錄$ PATH爲所有用戶:https://serverfault.com/questions/102932/adding-a-directory-to-path-in-centos

然而,這仍然不會被加載,因爲它是一個非交互式非登錄shell。

一個建議是添加路徑到/ etc/.bashrc中:How do I set $PATH such that `ssh [email protected] command` works?

但是這也沒有爲我工作。我相信它是因爲SSH不加載/ etc/bashrc。

另一個建議是編輯〜/ .ssh/environment:http://www.ruby-forum.com/topic/79248。然而,這與在deploy.rb中指定路徑幾乎一樣麻煩。

26

Bundler未找到,因爲.bash_profile未被加載,因此您的PATH錯誤。這可能是因爲你有.bash_profile中的RVM腳本。

簡單的答案是將RVM腳本從.bash_profile移動到.bashrc,並且Capistrano應該能夠找到它(還要驗證.bash_profile來源.bashrc)。

Capistrano使用SSH通過non-interactive shell在服務器上執行命令。此shell會話將source .bashrc but not .bash_profile。我向兩者添加了ECHO語句,並通過SSH運行LS。您可以在下面,只有.bashrc中的結果是來源看:

$ ssh [email protected] ls 
.bashrc loaded 
git 
file1 
file2 
+0

從你的'.bashrc'文件中找到'.bash_profile'是否是一個好習慣?這樣,無論是交互式還是非交互式外殼,您的'.bash_profile'始終都可以得到源代碼。 – user2490003 2015-06-10 22:11:22

7

沒有rvm/capistrano爲我工作。我發現最好的解決辦法是增加deploy.rb文件以下行(這是對於非全系統RVM):

set :bundle_cmd, 'source $HOME/.bash_profile && bundle'

+4

Hm ...現在獲取'sh:source:not found'錯誤。 – Vivek 2012-09-26 05:16:12

11

我不得不使用rbenv一個相同的問題。解決方法是從我的.bashrc文件的底部獲取rbenv特定行,並將它們放在頂部。如果shell沒有以交互模式運行,我的.bashrc文件的第一行將返回中止。

0

我嘗試了一些建議。在爲RVM環境設置deploy.rb文件中的路徑時遇到問題。我的最終解決方案是包括以下內容:

在config/deploy.rb文件中加入:

require "bundler/capistrano" 

另外在配置/ deploy.rb,或在我的情況的config/production.rb,因爲我是使用多級選項Capistrano的

after "deploy", "rvm:trust_rvmrc" 

這一步只是確保我們停止得到「你要信任.rvmrc文件」,它呼籲在部署任務。RB文件如:

namespace :rvm do 
    task :trust_rvmrc do 
     run "rvm rvmrc trust #{release_path}" 
    end 
end 

把這些細微的變化,我能夠運行cap production deploy其簽出的代碼後,執行資產管道部署,將發佈文件夾鏈接到當前,執行bundle install並清理。

1

這一個爲我工作:

集:bundle_cmd, '源$ HOME/.bash_profile中& &束'