all that is technology

GIT post receive hook


Whenever I make changes to my site, I end up doing lot of manual tasks deploying it to the production.

So I first moved the code to my self-hosted GIT server. This reduced most of the manual tasks, however deploying were still tedious.

This is where GIT's post-receive hook came handy. post-receive is called when the repository receives changes. Adding a little piece of code solves the deployment issue.

In GIT hooks folder, create a file post-receive if not already available.

$ cd path/to/git
$ cd repos/reponame.git/hooks
$ touch post-receive
$ chmod +x post-receive

add the following code to hook

GIT_WORK_TREE=/absolute/path/to/website git checkout -f
GIT_WORK_TREE=/absolute/path/to/website git reset --hard

In my case, I'm the only person who would be changing the repository and deploying it. So I do everything on master branch, and have not put much thought on having a development branch and production branch.

Be informed that, this hook is to just deploy and does not do any pre-deploy steps like preparing the environment or post-deploy steps like collectstatic or dbsync.