Apache SVN: Auto-Update Server Copy on Commit

It seems these days I spend more time figuring out annoying problems rather than doing any real work. The following gave me another big headache: Getting SVN to auto-update properly via it’s hooks. SVN provides you the ability to specify hooks after certain events occur with the repository in question. For example, say you have a live application under version control and you want SVN to auto-update the live copy (on the server) after every commit. I guess that makes sense, especially if you want to develop and test in your production environment, on the fly.

In the directory were your repository exists there should be a hooks directory. This directory includes a bunch of tmpl files that get called depending on the event that just occurred. The file that I cared about was the post-commit.tmpl file.

Assuming your running SVN using Apache, then most likely when your post-commit script gets called it will be executed using the user www-data. This is important because it means your repository needs to be checked out via the same user to ensure no permission problems occur. Assuming that OK, then add the following line to your post-commit.tmpl file:

Code:
/usr/bin/svn update <path to repo> --username xxxxxx --password xxxxxx

Hardcoding your SVN password in the file is probably not the best idea, but if your the only one who would have access to the file, then who cares. Notice how I put a full path to SVN, that’s because when the hook scripts get called all environment variables, etc. get removed. The next step, and probably most important, rename the file removing the .tmpl extension and change the ownership so the file becomes executable.

Code:
cp post-commit.tmpl post-commit
chmod +x post-commit

That should do it, so when you update your repository, SVN should auto-update your live working copy. Most likely you’ll run into problems, if so check the links below for more info.

Helpful Link #1
Helpful Link #2
Helpful Link #3

Related Articles:

Follow me on Twitter!

Your Ad Here

8 Responses to “Apache SVN: Auto-Update Server Copy on Commit”

  1. Ben Davies says:

    Wow, coincidence, I was trying to solve this exact problem today, and you have posted exactly what I was looking for. However, I have a couple of problems!

    I’m running several sites using virtual hosts on our server, with a repository for each. It’s my understanding that the server needs to initially checkout each repository into the root of each virtual host, is this correct?

    Is it possible for the server to have several working copies open at once?

    Cheers!

    Ben

  2. George A. Papayiannis says:

    Hey Ben,

    Yeah that’s correct, either that or check them out to some working folder and make a link (ln) to it from your virtual host directory. Each of your repositories would have their own hooks directory, so you’d have to setup individual hooks for each repo. Be sure that the initial checkouts on the server have the right permissions (www-data for me), so that Apache/SVN can run the hook correctly.

    Good luck,
    George

  3. Ben Davies says:

    Thats what I thought, though I’m having trouble with SVN UPDATE. I have a post-commit.bat file setup which calls my own php script (which will be used by all the virtual hosts, as the repository name and revision number are passed to the batch file, and passed on to the php script). This all works great (it currently emails me on each commit with the details).

    However, I wanted to add a simple call to SVN UPDATE to update the document root of the specified virtual host. When I enter SVN UPDATE in the cmd prompt, I get “Skipped ‘.’”. Should I be:
    - calling the SVN UPDATE from outside the working dir? (all my virtual hosts document roots are in a folder called ’server’ like so: c:/server/intranet.bob.local, so I would need to call ’svn update c:/server’)?

    Cheers!

    Ben

  4. Ben Davies says:

    Hi George,

    Sorry, I’m a dumbass. I needed to specify the full path as part of the svn update command. Doh!

    Thanks for your help though!

    Ben

  5. George A. Papayiannis says:

    Hey Ben,

    No problem, yeah I had that problem too. SVN removes all references to environment variables when those scripts are called.

    All the best,
    George

  6. Chris says:

    Config without using your password in your post commit.

    In your httpd.conf/apache.conf or virtual host config file where you created the svn web dav config.

    Simply insert something like this

    DAV svn
    SVNParentPath “/location/of/svn/repos”
    Order deny,allow
    Deny from all
    # only allow reading orders

    Allow from YOUR_IPADDRESS

    Inside the block if applicable.

    then instead of checking out this:

    /usr/bin/svn update –username xxxxxx –password xxxxxx

    USE:

    /usr/bin/svn update http://your-svn.url.com/private/reponame

    That way no passwords are used and adds a small layer of security of your system gets compramised.

  7. sihan says:

    hi
    How to do auto updates in SVN in windows platform.

    sihan

  8. Jon says:

    Thanks! It worked great and exactly what I was struggling with appreciate it!

Leave a Reply

Line and paragraph breaks automatic.
XHTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>