Search code examples
ruby-on-railsrubygitcapistranocapistrano3

Capistrano can't deploy from non-master branch


I'm using Capistrano (3.7.1) to deploy my Rails 5 application to a VPS. I have 2 major branches I'm using in git: master, for stable, production-ready code, and develop, for WIP code. I wanted to deploy the develop branch to a staging server, but it doesn't seem to be working.

In 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

In production.rb:

set :branch, 'master'

In staging.rb:

set :branch, 'develop'

Every time I try to deploy, it's failing, as follows:

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

But this is clearly not the case, as I'm getting:

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

which are clearly the same.

What's going on?


Solution

  • You are writing the commands which should run on the shell in single quotes which ruby is treating as String:

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

    instead of this:

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

    also you can use:

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

    %x also returns the standard output of running cmd in a subshell.