Publishing Articles with Git

This article will show you how to store the contents of your Nesta web site in a local Git repository, and publish changes by running a single git command.

We’ll setup two repositories; one on your local computer (so you can preview your changes locally before making them live) and one on your server.

Creating a local repository

We’ll make the local one first and copy any existing content into it. I like to keep mine inside my local copy of the nesta source code and to call it content (note that the content directory is ignored by git, so you needn’t worry about accidentally checking your articles into your local copy of the Nesta code).

On your workstation, type:

$ mkdir path/to/nesta/content
$ cd path/to/nesta/content
$ mkdir pages attachments
$ git init

If you already have any article or category pages copy them into these new directories now. Then add them to your git repository.

$ git add .
$ git commit -m "First commit."

Creating a remote repository

Now we can login to the server and create the remote repository:

$ ssh your.web.server
$ mkdir -p git/content.git
$ cd git/content.git
$ git --bare init

Back on your workstation, you can tell your local repository about your remote repository and then push all your content onto your server (this gives you a handy backup repository, should you accidentally lose your local repository in some kind of freak accident):

$ cd path/to/nesta/content
$ git remote add server ssh://your.web.server/home/user/git/content.git

Adjust the path in the above command accordingly – I like to store my content in my home directory, but you can put this repository anywhere you like.

Publishing via the remote repository

You’ve probably noticed that Nesta doesn’t know anything about your remote Git repository yet, so it won’t be able to serve any of your web pages. If you have a look inside the remote repository’s directory you’ll see a bunch of Git files, with no sign of your content in site. That’s because it’s a bare repository; don’t worry though – all your data is in there.

We’re going to write a git hook that will copy a fresh set of pages from your remote repository to /var/apps/nesta/shared/content (the default location for your content in production mode). It will also remove any cached .html files in your public directory. Replace the hooks/post-receive script in your remote repository with this script:

#!/bin/sh

NESTA="/var/apps/nesta"
SHARED="$NESTA/shared"
rm -rf "$SHARED/content.old" "$SHARED/content.new"
git archive --format=tar --prefix=content.new/ HEAD | \
    (cd $SHARED && tar xf -)
mv "$SHARED/content" "$SHARED/content.old"
mv "$SHARED/content.new" "$SHARED/content"
find "$NESTA/current/public" -name \*.html -or -name \*.xml | xargs rm

You’ll also need to make it executable:

$ chmod +x git/content.git/hooks/post-receive

Now (back on our workstation) we can try publishing a new page:

$ echo "# Test page" > pages/test.mdown
$ git add pages/test.mdown
$ git commit -m "Added test page."
$ git push server master

If everything has gone according to plan you should be able to see your new page at http://your.web.server/test.