Using Subversion for Rapid Website Development

Subversion has some excellent features, in this post we’ll be looking at how svn hooks can be used for rapid website development.

The majority of development work is done on the developers actually machine or a development team server. Normally with a working copy of the code in one directory, and sometimes a separate copy in another folder being served by Apache or IIS.

As the developer works on the code, they will often make some updates to their server directory to see their changes in effect. Often making lots of small changes, updating the server folder often is a pain. Here I will show you how to use svn hooks to automatically update a folders contents when you commit your latest version into the repository. This way your server will always be serving your latest work.

The svn repository folder structure contains five separate folders.

conf
dav
db
hooks
locks


Today we are just interested in the hooks folder. This contains templates for all the events fired as you commit and work with the svn repository. They include:

start-commit.tmpl
post-commit.tmpl
pre-commit.tmpl
post-unlock.tmpl
post-lock.tmpl
pre-revprop-change.tmpl
pre-unlock.tmpl
pre-lock.tmpl
post-revprop-change.tmpl

For this task we are interested in the post-commit.tmpl, as you can guess this is fired after every commit is made to the repository.

For *nix systems, rename you post-commit.tmpl to post-commit and give it permissions to execute.

For windows systems rename to post-commit.bat, permissions don’t tend to be a problem on Windows servers.

The fact that you have a file named this way is enough for Subversion to know to use your hooks, there is nowhere you need to configure anything to enable these features. Now we can create the script we want to execute whenever the event fires. Remember when svn runs these scripts it does it with NO environment settings (no %PATH% etc) so you will either need to reset your variables in the script or use absolute paths.

Here is an example of a Windows script (I don’t have a *nix box handy right now :( )

"C:\Program Files\Subversion\bin\svn" update C:\Checkouts\test-website --username test --password test

You can see that I’m calling svn with its absolute path, and that I’m doing and svn update to a folder. Because I want this script to run with no human action, I also add the arguments for username and password. If you are running a https repository you will need to accept the certificate manually once.

Now we have our hook will can do an svn checkout of whatever code we want, and on every commit Subversion will fire a command to update, if there are any modifications to make they will be made.

We can then add a simple alias to apache to access our website.

Alias /test "C:\Checkouts\test-website"

As this aimed at a development situation, the updates are mostly small and often. This means that you should not notice any loss of performance as you work.


About this entry