Sam Minnée

A random collection of stuff that I want online.

Home

Deployment using git push prod

17 June 2010 5:14pm. permalink

A good potential workflow for deployment with git: set up a remote repository called 'prod'. Then, do deploy your code you can just push to that.

To set it up, execute these commands on your production environment:

cd /your/web/root
git init
git config receive.denyCurrentBranch ignore
chmod +x .git/hooks/post-receive
echo "cd .." >> .git/hooks/post-receive
echo "env -i git reset --hard HEAD" >> .git/hooks/post-receive
echo "env -i git checkout -f" >> .git/hooks/post-receive

And then execute these commands on your development environment:

git remote add prod sminnee@sam.minnee.co.nz:/var/www/samminnee
git config remote.prod.fetch +master:master
git push prod master:master

One caveat here is that any uncommitted changes on your production environment will be clobbered by the post-update hook. Consider it a work in progress :-).

Deployment build scripts

Post-deployment scripts could potentially be loaded into hooks/post-update. Or, more to the point, hooks/post-update could be configured to run the correct script within the repository.

The way that I chose to do this is with a Makefile. Although Makefiles are a little crude, they are a useful way of aggregating together simple maintenance scripts.

echo "make post-deploy" >> .git/hooks/post-receive

The makefile that I have used for this site is quite simple. It checks sapphire out from subversion, and then runs sapphire's dev/build script to update the database.

post-deploy: 
    if [ -d sapphire ]; then svn update sapphire; else svn checkout http://svn.silverstripe.com/open/modules/sapphire/trunk sapphire; fi
    ./sapphire/sake dev/build

The process for deploying your site is then very simple. Simply call this from your development environment, after checking your new code into the master branch:

git push prod

I've set this up on sam.minnee.co.nz, testing and refining it during the process of writing this article.