Git for websites / after receiving / separation of test and production sites

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 
+5
source share
3 answers

Would the best approach be to have a base repository - this is a test repository site (with the corresponding working directory), and then the repository makes changes to the new live site, which has the corresponding working directory live site database? It would also allow me to move production to another server and the whole deployment chain.

Yes, definitely. This is a very rare case when you want your test site to be located right next to your production site. It is dangerous and unprofessional in almost all respects, rather than talking about database corruption, web server locks, etc.

I usually have a virtual machine setup for testing. It works very well and I can have it with me on my laptop while traveling.

Using git to deploy your website is a very good idea, there are many other people who do this (like Rob Coniri). If in any case you have live and test sites, you should have separate branches for them in your repository, configured as remote tracking branches in the corresponding server repositories. Your workflow becomes as easy as working in a test branch, pushing it for testing, testing, compressing and living live.

Honestly, do not do it too hard for yourself.

+1
source

Think both ways will work.

You can also use the "git archive master | tar -C c: / temp / BLAH -x" and the "git archive live-site | ssh live-site" tar -C / var / www -x "".

Saving separate repositories may be useful, but “pushing another hook associated with the connection” looks difficult, and I expect it to be slow. A variety of long chain that will be slow and brittle.

Can current site updates be updated after the launch of the "test" version?

+2
source

I also followed the same guide on toroid.org, but I would like to point out that although you started from an open repository by adding a working directory, you will likely need additional processing. I found that the following hook is useful if you have content that can change dynamically or otherwise and does not want to lose data when using git checkout -f

pre get

 #!/bin/sh git add -A git diff --quiet --cached if [ $? -gt 0 ]; then git commit --quiet -m "autocommit" echo "Working Directory was out of sync. Pull to receive updated index." exit 1 fi 

This will stop pressing if there are changes in the remote working folder. Think about it, as someone (the web server) is making changes, but forgets to commit them. Using checkout with -f discards these changes. This hook is a good place to prevent this from happening, but it would be nice if there was a hook called on the remote server to the exit so that you can easily get these changes.

after taking

 #!/bin/sh git checkout -f echo "Working directory synced." 

Regarding the two branches, I thought your first solution was more elegant than dealing with multiple repositories. If you really want to isolate your production site, you can use local rsync, which has a similar delta fix. I will have a test and stable branch in the repository, and only the testing site will work as a working directory. When you are ready to release, merge the testing into a stable branch, click and find the hook that is looking for a commit for the stable branch, call rsync.

+1
source

All Articles