【问题标题】:Capistrano can't deploy from non-master branchCapistrano 无法从非主分支部署
【发布时间】:2017-01-09 09:59:28
【问题描述】:

我正在使用 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

显然是一样的。

发生了什么事?

【问题讨论】:

  • 换行试试看:unless `git rev-parse HEAD` == `git rev-parse origin/#{fetch(:branch)}`

标签: ruby-on-rails ruby git capistrano capistrano3


【解决方案1】:

您正在编写应该在 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 还返回在子 shell 中运行 cmd 的标准输出。

【讨论】:

    【解决方案2】:

    使用反引号在脚本中执行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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      • 1970-01-01
      • 2016-04-21
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多