4

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?

dp7
  • 6,651
  • 1
  • 18
  • 37
mindseyeblind
  • 119
  • 10
  • Change the line to this and try: `unless \`git rev-parse HEAD\` == \`git rev-parse origin/#{fetch(:branch)}\`` – Deepesh Jan 09 '17 at 10:17

2 Answers2

4

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.

Deepesh
  • 6,138
  • 1
  • 24
  • 41
2

Use backticks to execute git commands in your script:

# 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
dp7
  • 6,651
  • 1
  • 18
  • 37