Simple Git-based multi-server deployments

Ansible is used to manage most of Midwestern Mac's infrastructure and deployments, and while it's extremely easy to use, there are a couple situations where a project just needs a little code to be updated across two or more servers, from a central Git repository, or from one master application server.

All Git repositories include a hooks folder, which contains sample git hook scripts. Inside this folder are a series of sample hook files like post-commit.sample and pre-rebase.sample. If you add a shell script of the same name as any of these files (excluding the .sample) to this folder, Git will run the script when the particular action runs (e.g. git will run a post-commit script after a commit).

Instead of deploying a codebase to multiple servers by copying it manually via SFTP or rsync, or by logging into the servers and pulling down changes from a remote Git server, you can add a post-receive script in the hooks folder of your main git remote repository, and have it synchronize the codebase with one or more remote servers:


# Check out the code on this server.
GIT_WORK_TREE=/opt/my-app git checkout -f

# Define an array of servers to which the code will be deployed.

# Push codebase to the servers via rsync.
  /usr/bin/rsync -av --delete --exclude file-to-exclude.txt -e ssh /opt/my-app/ username@$SERVER:/opt/my-app/

I wouldn't use this except for the simplest of configurations or in rare situations where Ansible (or another deployment solution) wouldn't be suitable... but it does the job well. You'll need to make sure the user under which git is run has an SSH key configured to connect to all the remote servers.

Read more about Git Hooks on, and if you're interested in using Ansible for your deployments, check out my book, Ansible for DevOps!


This is way simpler and easier to manage than Ansible, so it would actually be ok to use for larger deployments.