2014-10-04 9 views
0

我正在將我的Rails應用程序升級到Capistrano 3,我似乎碰到了牆。部署時,「未定義的局部變量或方法分支'爲#<SSHKit :: Backend :: Netssh:>」

當運行cap staging deploy,我得到這個錯誤:

** Invoke staging (first_time) 
** Execute staging 
** Invoke load:defaults (first_time) 
** Execute load:defaults 
** Invoke bundler:map_bins (first_time) 
** Execute bundler:map_bins 
** Invoke deploy:set_rails_env (first_time) 
** Execute deploy:set_rails_env 
** Invoke deploy:set_rails_env 
** Invoke deploy (first_time) 
** Execute deploy 
** Invoke deploy:starting (first_time) 
** Execute deploy:starting 
** Invoke deploy:check (first_time) 
** Execute deploy:check 
** Invoke git:check (first_time) 
** Invoke git:wrapper (first_time) 
** Execute git:wrapper 
** Execute git:check 
** Invoke deploy:check:directories (first_time) 
** Execute deploy:check:directories 
** Invoke deploy:check:linked_dirs (first_time) 
** Execute deploy:check:linked_dirs 
** Invoke deploy:check:make_linked_dirs (first_time) 
** Execute deploy:check:make_linked_dirs 
** Invoke deploy:check:linked_files (first_time) 
** Execute deploy:check:linked_files 
** Invoke deploy:set_previous_revision (first_time) 
** Execute deploy:set_previous_revision 
** Invoke deploy:started (first_time) 
** Execute deploy:started 
** Invoke deploy:check_revision (first_time) 
** Execute deploy:check_revision 
cap aborted! 
SSHKit::Runner::ExecuteError: Exception while executing on host %HIDDEN_HOST_NAME%: undefined local variable or method `branch' for #<SSHKit::Backend::Netssh:0x007fe8958826f8> 
config/deploy.rb:35:in `block (3 levels) in <top (required)>' 
/Users/user/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:54:in `instance_exec' 
/Users/user/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:54:in `run' 
/Users/user/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/runners/parallel.rb:13:in `block (2 levels) in execute' 
NameError: undefined local variable or method `branch' for #<SSHKit::Backend::Netssh:0x007fe8958826f8> 
config/deploy.rb:35:in `block (3 levels) in <top (required)>' 
/Users/user/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:54:in `instance_exec' 
/Users/user/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:54:in `run' 
/Users/user/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/sshkit-1.5.1/lib/sshkit/runners/parallel.rb:13:in `block (2 levels) in execute' 
Tasks: TOP => deploy:check_revision 
The deploy has failed with an error: #<SSHKit::Runner::ExecuteError: Exception while executing on host %HIDDEN_HOST_NAME%: undefined local variable or method `branch' for #<SSHKit::Backend::Netssh:0x007fe8958826f8>> 
** Invoke deploy:failed (first_time) 
** Execute deploy:failed 

這裏是我的Capfile:

# Load DSL and Setup Up Stages 
require 'capistrano/setup' 

# Includes default deployment tasks 
require 'capistrano/deploy' 

require 'capistrano/bundler' 
require 'capistrano/rails' 
require 'capistrano/rails/migrations' 
require 'capistrano/rails/assets' 

require 'new_relic/recipes' 

# Loads custom tasks from `lib/capistrano/tasks' if you have any defined. 
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r } 

Rake.application.options.trace = true 
Rake.application.options.backtrace = true 

這裏是deploy.rb

# config valid only for Capistrano 3.1 
lock '3.2.1' 

set :application, "%HIDDEN%" 
set :repo_url, "%HIDDEN%" 

ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp } 
# I've also tried simply set :branch, "master" but same result 

set :git_enable_submodules, 1 

set :bundle_flags, "--deployment --verbose" 

set :rails_env, "production" 

set :pty, true 

set :linked_files, %w{config/database.yml} 
set :linked_dirs, %w{bin log tmp vendor/bundle public/system} 
set :config_files, %w{database.example.yml} 

而且staging.rb

location = "%HIDDEN_HOST_NAME%" 

role :app, location 
role :web, location, no_release: true 
role :db, location 

server location, roles: %w{web app db} 

set :ssh_options, { 
    user: "deploy", 
    forward_agent: true, 
} 

http://capistranorb.com/documentation/getting-started/cold-start/,我可以運行cap staging check_write_permissionscap staging git:check就好。那裏沒有錯誤。運行cap production deploy會產生相同的錯誤。

任何想法?提前致謝!

回答

2

原來我有一個任務是指#{branch}。這顯然在第2章但不是第3章。

這是修復:

desc "Make sure local git is in sync with remote." 
    task :check_revision do 
    branch = fetch(:branch) # <--- This line fixed it 
    on roles(:app) do 
     unless ENV["IGNORE_GIT_SYNC"] 
     unless `git rev-parse HEAD` == `git rev-parse origin/#{branch}` 
      puts "WARNING: HEAD is not the same as origin/#{branch}" 
      puts "Run `git push` to sync changes." 
      puts "(set IGNORE_GIT_SYNC=1 to ignore this)" 
      exit 
     end 
     end 
    end 
    end 
相關問題