Git Hooks To Automate Deployment

Deploying your git repository into production can be made a lot easier by using Git hooks. In this article I’ll be showing you how to use the post-receive hook to deploy your repository into your production environment.

It will only deploy your master branch, which should be your stable code. If you attempt to push a different branch nothing will be updated.

Prerequisites

Before you get started, please ensure you have git installed on your server and that you are familiar with the git commands through the command prompt. You’ll also need to ensure you have access to your production environment along with a git repository to use.

Setup your production environment

First you need to access your production environment through the command prompt and initialize a bare git repository within your html directory:

cd /home/public_html
git init --bare project.git

This will setup a bare git repository within the directory project.git. Now you need to create a new file to setup the post-receive hook:

vim project.git/hooks/post-receive

Add the following script inside the file modifying it to suit your directory:

#!/bin/bash
while read oldrev newrev ref
do
  if [[ $ref =~ .*/master$ ]];
  then
    echo "Master ref received. Deploying master branch to production..."
    git --work-tree=/home/public_html --git-dir=/home/public_html/project.git checkout -f
  else
    echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server."
  fi
done

Here you’ll see it checks if the reference is the master branch using an if statement. It will display the relevant message to you within the command prompt when the repository is pushed to the production server.

--work-tree directory is where your code will be added from your repository.
--git-dir directory is where your bare git repository is located.

Now you need to make sure the file is executable for the hook to work:

chmod +x project.git/hooks/post-receive

The production server is now all setup and ready to receive your updates.

Setup your local environment

Locate your local repository and add the production environment as a new remote:

cd /Users/your_username/Sites/project
git remote add production ssh://[email protected]/home/public_html/project.git

The important thing to note is that the directory url needs to match up to the location of the bare git repo on your production environment.

To check your new remote has been added correctly you can view all remotes by typing in:

git remote -v

Deploy

The final step is to deploy your code by pushing to the new remote you just added:

git push production master

If successful you should see something similar to the output below:

Counting objects: 2, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 229 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Master ref received. Deploying master branch to production...
To ssh://[email protected]/home/public_html/project.git
   a8b336f..ee1e27f  master -> master

Your code has now been deployed into your production environment.

Every time you now want to update your production environment you don’t need to login to the server and manually pull your changes, simply push to your production remote and the git hook will take care of the rest for you.