Use Git Hooks to Push to Live Site

There are too few tools and docs for Git, but the few that exist are awesome. Many developers take the easy and quick approach to working with git by setting up an account with Github and then using tools like gitx to visually manage and track their repos. While I do love this approach for many reasons, there are many reasons to setup your own git hosting and tracking…

  • Understand how git actually works and what resources it uses
  • Learn more advanced techniques when you do it yourself
  • Save money (github and most other services charge if you want private repos)

It took me about a week to fine-tune and get everything setup the way I wanted, but the end result is:

  • A privately managed git repo that looks and functions like Google Code
  • Custom scripts which update my live site when the live branch is update
  • My own agile development setup which allows me to manage users quickly and easily
  • A visualizer that makes git easy to understand at any level

Step 1: Install Git

This step is pretty easy if you google it.

Step 2: Install Indefero

If you go to their website you can download their open source project and set it up using their instructions. Feel free to ask here if you need help with this step.

Step 3: Test your Git Repo

Setup a new project in Indefero and grab their code to clone the repo. You may need to setup a custom user with access to the indefero/ directory. For me, I created a user titled git and gave the user shell access to my /var/local/indefero directory.

Once you’ve setup the repo, clone it to your local machine drop in a test file and push to make sure indefero and git are working correctly.

Step 4: Setup Post Update Hooks

Git provides hooks for running shell scripts on predefined events. I used the post-update hook on my live repo to use my webroot (where my physical site resides) for the worktree. This allows me to track changes to the live site (which makes roll-backs or recovery a breeze) as well as run any other commands such as a mysql dump everytime the live site is updated. Here’s what you do to set this up:

Navigate to indefero/git/repositories/YOUR_WEBSITE/ in terminal, then create a script named post-update:

nano hooks/post-update

Put this into your script, along with any other commands you’d like:

#!/bin/sh
git checkout -f

Finally, edit your config file using git config or nano config:

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        worktree = /path/to/YOUR_WEBSITE
[receive]
        denycurrentbranch = ignore

This will tell git that this repo uses your custom directory as it’s worktree (where the physical files are located) and the post-update hook does a checkout everytime you update your repo.

Hopefully it helps, if you need more hit the comment form. I will most likely come back and expand on this post with more detailed instructions if I get enough feedback or requests…

This entry was posted in Technology and tagged , , , . Bookmark the permalink.