Atomic website update with Git
If I simply copy the changed files into the website directory, then at least a few requests will see an inconsistent site. If I copy the new site to a separate directory, then rename the old directory to take it out of production and rename the new directory to make it live, then for one brief moment we’ll get 404s.
I really have two questions:
How can I update a directory of files in an atomic way?
How can I coordinate this with Git? We’d like to deploy using a git pull (or possibly push). The path of the site within the git repo is different from the path on the server, so a bit of moving/renaming needs to happen, either using git commands or OS commands.
3 Solutions collect form web for “Atomic website update with Git”
You can do what you’re imagining using symlinks. Moving one symlink over another is an atomic operation, so you should be able to avoid any 404 errors.
Your hook would deploy your site to a directory, perhaps named after the commit hash. Then it would make a symlink to that, perhaps called staging. Then it would move that symlink over the production symlink.
hash=`git rev-parse HEAD` git checkout-index -a -f --prefix=/srv/www/$hash/ ln -s /srv/www/$hash /srv/www/staging mv -T /srv/www/staging /srv/www/production
Like Owen said, you may use a symlink to the last release you deployed. I’ve written a blog post about this recently: http://bugfixer.endel.me/2013/08/04/simple-atomic-deployment-with-git
You may configure a post-receive hook on your remote git server that creates the symlink when the new release is ready.
I am not totally sure about it, but you could create post push hook for git, which creates new dir, or copy existing one, pull or clone your project and change link from your project to new dir. Your project dir is just link to one of deployed dirs.
But my method sounds like workaround.