I use Git to manage the source code and the deployment of my website, and currently I have test and live sites working in the same field. After this http://toroid.org/ams/git-website-howto resource, I originally came up with the following post-receive script hook to split between clicks on my live site and clicks on my test site:
while read ref do #echo "Ref updated:" #echo $ref -- would print something like example at top of file result=`echo $ref | gawk -F' ' '{ print $3 }'` if [ $result != "" ]; then echo "Branch found: " echo $result case $result in refs/heads/master ) git --work-tree=c:/temp/BLAH checkout -f master echo "Updated master" ;; refs/heads/testbranch ) git --work-tree=c:/temp/BLAH2 checkout -f testbranch echo "Updated testbranch" ;; * ) echo "No update known for $result" ;; esac fi done echo "Post-receive updates complete"
However, I have doubts that this is really safe :) I'm not at all a Git expert, but I assume that Git probably tracks the current statement branch, and this approach can probably confuse him to the end.
So a few questions:
Is it safe?
Would the best approach be for my base repository to be the repository of the test site (with the appropriate working directory) and then change this repository to a new repository of the real site that has the corresponding working directory for the live site database? It would also allow me to transfer production to another server and save the whole deployment chain.
Is there something I am missing? Is there any other clean way to distinguish between test and production deployments when using Git to manage websites?
As an extra note in light of Vi's answer, is there a good way to do this that will handle deletions without repeatedly deleting the file system?
Thanks, -Walt
PS - script I came up with for a multiple repository (and use it if I don’t hear better) looks like this:
sitename=`basename \`pwd\`` while read ref do #echo "Ref updated:" #echo $ref -- would print something like example at top of file result=`echo $ref | gawk -F' ' '{ print $3 }'` if [ $result != "" ]; then echo "Branch found: " echo $result case $result in refs/heads/master ) git checkout -q -f master if [ $? -eq 0 ]; then echo "Test Site checked out properly" else echo "Failed to checkout test site!" fi ;; refs/heads/live-site ) git push -q ../Live/$sitename live-site:master if [ $? -eq 0 ]; then echo "Live Site received updates properly" else echo "Failed to push updates to Live Site" fi ;; * ) echo "No update known for $result" ;; esac fi done echo "Post-receive updates complete"
And then the repo in .. / Live / $ sitename (these are “bare” repositories with work trees added after init) has a basic post-reception:
git checkout -f if [ $? -eq 0 ]; then echo "Live site `basename \`pwd\`` checked out successfully" else echo "Live site failed to checkout" fi
source share