Saturday, August 20, 2011

Git Deployment Magic

I got nothing against [insert your favorite Git host here] but sometimes I find the cost unwarranted for simple projects and their tool set more then unnecessary for laid out waterfall development. So here I am going to demonstrate the following:
- Setup a remote git environment with auto deploy.
- Setup a local environment.
This process involves quite a bit of shell work so if you've never used a shell and your world is all about finding the cleanest GUI I did my best to break down the commands.

Setting up a remote environment
1) Create two directories, src and bin. The src directory will be where the git repository resides and bin where your website is deployed. These can be called anything and don't even have to reside in the same folder.
Make sure to get the path of the src directory by going in there and typing 'pwd'.
2) Initialize your src directory by running: 'git init --bare'
3) Create a post deploy hook via 'cp hooks/post-receive.sample hooks/post-receive'
4) Edit hooks/post-receive and add the following:
cd ../bin && env -i git pull
The env -i is necessary because git will always try and execute the command in the directory specified by GIT_DIR. If your bin directory is somewhere else make sure to specify that in cd (eg. cd /home/test/one/bin). To get full path type 'pwd'. For most people the bin directory will be where the host specifies the HTML/PHP/etc files go.

Setting up the development environment
1) Go to your local directory where the code will live and run: git clone user@ . and user need to be replaced with your remote server location information. I find all this information is much easier to store in .ssh/config and create a private/public key for authentication to skip password entry every time. Refer to google on how to do that.
2) After this your repository should be successfully checked out locally. Add a first few files and try the checkin process via: git push origin master.
This will create your first branch and push it to the server. When the push is done a copy will be automatically checked out to your bin deployment folder. Any errors in the process will show up during push and you have to fix accordingly.
NOTE: After your first push you have to manually clone the src into bin: 'git clone ../src .'

Hopefully this helps someone out and if you notice any errors, issues let me know,