5

I've written a very simple 'deploy' script running as my post-update hook inside my bare git repo.

The variables are as follows

live domain         = ~/mydomain.com
staging domain      = ~/stage.mydomain.com
git repo location   = ~/git.mydomain.com/thisrepo.git (bare)

core                = ~/git.mydomain.com/thisrepo.git
core                == added remote into each live & stage gits

both live & stage have initialized git repos (non-bare) and I have added my bare repo as a remote to each of them (named core) so that git pull core stage or git pull core live would pull the updated files from the respective branch of the core repo.

The script is as follows:

#!/usr/bin/env ruby

# Loop over each passed in argument
ARGV.each do |branch|

  # If it matches the stage then 'update' the staging files
  if branch == "refs/heads/stage"

    puts ""
    puts "Looks like the staging branch was updated."
    puts "Running a tree checkout now…"
    puts ""
    `cd ~/stage.mydomain.com`
    `unset GIT_DIR` # <= breaks!
    `git pull core stage`
    puts ""
    puts "Tree pull completed on staging branch."
    puts ""

  # If it's a live site update, update those files
  elsif branch == "refs/heads/live"

    puts ""
    puts "Looks like the live branch was updated."
    puts "Running a tree checkout now…"
    puts ""
    `cd ~/mydomain.com`
    `unset GIT_DIR` # <= breaks!
    `git pull core live`
    puts ""
    puts "Tree checkout completed on live branch."
    puts ""

  end

end

I have tried adapting the 'updating' of files from this bash script here which uses the unset GIT_DIR to run the next git command git pull core stage for example. core is the added remote of my bare repo in a different folder on the server.

However when executing the script above I'm getting the following errors:

remote: hooks/post-update:35: command not found: unset GIT_DIR        
remote: fatal: /usr/lib/git-core/git-pull cannot be used without a working tree.        

Is there any way to do the same thing as unset GIT_DIR in a bash script within my ruby script?

Many thanks,

Jannis

Jannis
  • 17,025
  • 18
  • 62
  • 75

2 Answers2

6

This looks like

`cd ~/stage.mydomain.com && unset GIT_DIR && git pull core stage`

could do the job.

Speculating on why (speculating as I am not familiar with ruby): you were running the unset command in a different shell from the one which runs the git pull (and as samold points out in his answer, the same issue happens with the current working directory).

Which suggests that there might be some ruby API which manipulates the environment ruby passes to the shell it launches with the backtick operator, and also to change the current working directory.

ndim
  • 35,870
  • 12
  • 47
  • 57
  • This works like magic! Thank you very much. Got it all up and running now! Off to find out how to do the same by passing in a specially formatted comment into the commit notes :) I wonder if something like `[update:live]` or `[update:stage]` could be used to trigger this `pull` action… – Jannis Apr 03 '11 at 00:41
  • Just saved me a ton of frustration - Thank you! Didn't realize these variables had such a significance! – TJ Biddle May 28 '13 at 18:31
5

Try replacing your line with this:

ENV['GIT_DIR']=nil

I'm not sure your:

`cd ~/stage.mydomain.com`
`unset GIT_DIR` # <= breaks!
`git pull core stage`

sections will work even when the GIT_DIR has been properly unset; each backticks starts a new shell, unrelated to the old shell, and child shells cannot change the current working directory of their parent processes.

Try this:

ENV["GIT_DIR"]=nil
`cd ~/stage.mydomain.com ; git pull core stage`
sarnold
  • 102,305
  • 22
  • 181
  • 238
  • Thanks for the tip! This is equally as good as __ndim__'s answer but because my final script is using his single string in back ticks formatting I chose to accept his answer instead of this one. Regardless many thanks again! – Jannis Apr 03 '11 at 00:42
  • 1
    @Jannis, depends upon your goals: @ndim's answer is better if you want to unset `GIT_DIR` for that _one_ invocation only. Mine is better if you want `GIT_DIR` unset for the entirety of the script. They do different things :) so pick whichever one fits the task better. – sarnold Apr 03 '11 at 00:46
  • Oh,.. now that you mention it, that does make complete sense! Thanks for letting me know about this, in this instance I will keep it as `unset ..` but for future scripting adventures this will definitely come in very handy. Thanks again. – Jannis Apr 03 '11 at 01:00
  • IMO this is the superior solution for most cases; e.g. where the build environment should be clean for the whole build. I recognize that some builds may perform stepwise ENV changes, but I would argue that's not the best practice. I've implemented @sarnold's solution in our build environment. – New Alexandria Jul 03 '12 at 23:26