部署时,"undefined local variable or method `branch' for #<SSHKit::Backend::Netssh:>"



我正在将Rails应用程序升级到Capistrano 3,我似乎遇到了麻烦。

当运行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会产生相同的错误。

有什么想法吗?提前感谢!

原来我有一个任务引用了#{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

相关内容

最新更新