Capistrano 无法从非主分支部署

Capistrano can't deploy from non-master branch

我正在使用 Capistrano (3.7.1) 将我的 Rails 5 应用程序部署到 VPS。我在 git 中使用了 2 个主要分支:master,用于稳定的生产就绪代码,develop,用于 WIP 代码。我想将 develop 分支部署到暂存服务器,但它似乎不起作用。

deploy.rb中:

# This is the failing task
task :check_revision do
  on roles(:app) do
    unless 'git rev-parse HEAD' == "git rev-parse origin/#{fetch(:branch)}"
      puts "WARNING: HEAD is not the same as origin/#{fetch(:branch)}"
      puts 'Run `git push` to sync changes.'
      exit
    end
  end
end

production.rb中:

set :branch, 'master'

staging.rb中:

set :branch, 'develop'

每次尝试部署都失败,如下:

$ cap staging deploy
... initial steps, skipped over ...
WARNING: HEAD is not the same as origin/develop
Run `git push` to sync changes.

但事实显然并非如此,因为我得到:

$ git rev-parse HEAD
38e4a194271780246391cf3977352cb7cb13fc86
$ git rev-parse origin/develop
38e4a194271780246391cf3977352cb7cb13fc86

明显是一样的

怎么回事?

在脚本中使用反引号执行 git 命令:

# This is the failing task
task :check_revision do
  on roles(:app) do
    unless `git rev-parse HEAD` == `git rev-parse origin/#{fetch(:branch)}`
      puts "WARNING: HEAD is not the same as origin/#{fetch(:branch)}"
      puts 'Run `git push` to sync changes.'
      exit
    end
  end
end

您正在用单引号在 shell 上编写应该 运行 的命令,其中 ruby 被视为 String:

unless 'git rev-parse HEAD' == 'git rev-parse origin/#{fetch(:branch)}'

而不是这个:

unless `git rev-parse HEAD` == `git rev-parse origin/#{fetch(:branch)}`

你也可以使用:

unless %x{git rev-parse HEAD} == %x{git rev-parse origin/#{fetch(:branch)}}

%x 也 returns 运行ning cmd 的标准输出 shell.