bloghow2doitde

A blog about computer science and a life between Heidelberg and Munich

To have a nice, small repository to upload and track the development of your blog is a great thing. But it would be even greater if it is deployed automatically after every git pull command. This article describes the deployment of a Octopress based blog. But it’s easy to extend those instructions or even to rewrite them for working together with Jekyll or other static site generators.

If you’re using a remote repository like they are created from gitolite and similar software, there is a problem: we do need a local repository to do a rake generate. Otherwise Rake is not able to find his Rakefile.

So the solution is to create a temporary, local clone of the git repository first. After this is done we can create our static web-pages and move them to our desired web directory. Of course we don’t want to do all that manually! Because of this we have to tell gitolite to do all that stuff automatically. This is why we’ll have to pack it together in a small script. I choose bash but of course one could also use Python or every other language. The only important thing is to save the script as as hooks/post-receive within the root directory of the repository you want to build automatically.

/path/to/repo.git/hooks/post-receive
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/sh

PUBLIC_HTDOCS=/path/to/public/htdocs
TMP_DIR=$(mktemp -d)
CLONE_BASE=$TMP_DIR/myrepo.git
REPO_BASE=/path/to/repo-base

/usr/bin/git clone $REPO_BASE/myrepo.git $CLONE_BASE
cd $CLONE_BASE
/usr/bin/rake generate

cp $CLONE_BASE/public/* $PUBLIC_HTDOCS/ -Rf

chown gitoliteUser:HttpdUser $PUBLIC_HTDOCS -Rf
find $PUBLIC_HTDOCS -type f -print0 | xargs -0 chmod 644
find $PUBLIC_HTDOCS -type d -print0 | xargs -0 chmod 755

rm $TMP_DIR -rf

I used mktemp -d to create a unique named directory within the local temporary directory. In this newly created directory git clone will generate another directory containing the cloned repository. If git is done we’re able to change into the now available directory containing a local mirror repository and wake up Ruby by calling rake generate.

After the generation (which can take a few seconds depending on the size of the blog) we simply have to copy the newly generated websites to a directory accessible by our httpd. If you need special permissions you can set them with chown and find afterwards.

Now the deployment is done and we can delete our temporary directory. That’s all it takes to get gitolite to build our blog on every push we do.

posted in: Linux, Octopress, git, gitolite, jekyll, sass