Using Git hooks to deploy your application is simple, this is a “git push to deploy” tutorial. Of course you need to use Git as your version control system, but hopefully you are already using it. You can achieve pretty much anything with your deploys with a simple setup. If you want to deploy your application with a simple
git push to your production server and automate all the necessary steps.
Quite simple actually. On your local environment your application will be located in
~/dev/app and on your production server it will be located in
/var/www/app.git. This is all we need to know. Let’s go.
There are a numerous of different hooks available for you. 17 ones to be specific: applypatch-msg, pre-applypatch, post-applypatch, pre-commit, prepare-commit-msg, commit-msg, post-commit, pre-rebase, post-checkout, post-merge, pre-receive, update, post-receive, post-update, pre-auto-gc, post-rewrite and pre-push.
With these hooks you are able to hook in and perform commands before or after certain actions in Git. This could either be client or server side. The pre-commit hook is a client side one and will be triggered before making a commit. Want to reject commits that does not comply with your coding standard? Not a problem. The update hook on the other hand is a server side one and will be triggered on the remote just before it updates the refs when you do a push. In this we will focus on post-receive and post-update which both are server side hooks.
The hooks resides in
.git/hooks of your repository. Therefore they are not in the actual repository. But if you want to version control them you can create a
hooks folder in your repository and create a symbolic link to
.git/hooks from that. Each and every one of your hooks needs to be executable in order to work properly.
One thing to mention about running hooks are that the their working directory when executing is the
.git folder (the
GIT_DIR environment variable). This can be unset or you can change the working directory in the actual hook itself.
One of the best features about Git hooks is that they don’t care what language they are written in. As long as they are executable everything is fine.
Like Bash? Not a problem.
#!/bin/bash echo "Hello World!"
Prefer Python? Sure.
#!/usr/local/bin/python print "Hello world"
I’ll stick to Bash in my examples, since then we don’t have to worry about a specific language being installed on the system.
Set up your server for a git push to deploy
Fire up a terminal and log in remotely to your server. We’ll start off setting up an empty repository in
mkdir -p /var/www/app.git cd /var/www/app.git git init
This is all we need to do on the remote for now. Of course you need to set up your web server to serve that folder for some domain. If you’re reading this I’ll assume that you are proficient in configuring your web server.
Set up your local environment
Once more fire up the terminal if you don’t have it open. Let’s create our local repository. You can skip this step if you already have your repository ready.
mkdir -p ~/dev/app cd ~/dev/app git init
Now you have a local repository. We need to connect that to the server, so we want to add a remote pointing to it.
git remote add production [email protected]:/var/www/app.git/
You have now successfully created a set up where you can push from your local environment straight to your server! Let’s try it and see that it works.
echo " * Abort deploys on errors * Updating front end dependencies * Making builds * Running test suites * Cache busting * Updating revision numbers for static assets * Database migrations * Notifications of (un)successful deploys
I hope this short and sweet git push to deploy tutorial opened up a door to its potential for you.