I'm trying to use this post-recieve hook to update my live server
GIT_WORK_TREE=/var/www/www.example.org git checkout -f
This hook is on the remote bare repo and I would like the live work tree to be on a different server.
How do I set this up so that when the hook fires it checks out the files to the live server, does it ssh from the remote server to the live server? Where do I define this?
Here's what I'm trying to setup
http://www.dejaaugustine.com/2011/05/leveraging-git-as-a-full-fledged-web-development-tool/
but live and test sit on separate servers to the bare repo, but instead of using git pull I was going to use git checkout -f
.
You will have to update your post-receive hook to get the files from the checkout folder in your git server and scp it to your live server.
You could setup a bare repository on live/test servers and push to them with a post-receive hook from the main origin repository. Then again use post-receive hook to checkout to the web directory.
Or use rsync for the main origin repository to live/test servers. (Be careful with time lag.)
The following solution updates a live server code base with code from a bare repo on another server. This solution does not use scp or copy files to overwrite on the live server, because we want to avoid overwriting a whole directory (with git we can choose what we want to update).
Assumptions:
On the live server:
cd /var/www/www.yoursite.com && git init
git remote add origin git@testserverip:/path/to/repo.git
git fetch origin
followed bygit merge
Since this is a live server, you typically do not want any merge-conflicts to cause trouble. If you don't care about loosing changes on the live server (because you probably never change anything important on the live server), you can use
git merge -m 'Overwriting live server' -s recursive -X theirs origin/active-branch-on-live-server
A typical scenario is that you have other files (temp files, user-changed files etc) on the live server that you do not want to overwrite). Make sure to add all these files/directories to your .gitignore-file and make sure they are not added by git add. That way, they will not be affected when code is pulled from you centralized repo.
If you want this setup to me more automatic, make a bash script on the live server:
Make this script as an executable bash script (not covered here). Now, you can call this script from a hook-script on the "centralized" server, so that the live server gets updated every time you push your code.
On the "centralized" repo/test/staging server:
(You should already have a bare repo set up here, if not create it).
In your bare-repo.git/hooks/ create/edit the
post-receive
file, so that the live server runs the script created above when code is pushed to the bare repo:Make sure that the git user on you the server that hosts your bare repo has access to your live server via ssh-keys, so that the ssh in the script above works.
This is a schematic overview. Details can be found other places: