svn post-commit hook : update only if certain file has changed

3.1k views Asked by At

Okay so I have an SVN repo setup on my server. I have a post-commit hook setup that updates to a "DEV" folder so that when I commit changes, it automatically pushes to my "DEV" subdomain on my server.

What I need now is an easy way to push it to my "Live" subdomain when I am ready. The "Live" subdomain is on the same server, and in reality, it would be the same svn update /... command as with the DEV, but to a different path.

I was thinking I could have a specific file in my repo, and maybe I could just change this file, and then make some sort of condition in my post-commit file to also update to my "live" dir if that specific file has changed.

But I don't know how to write that code. Currently my post-commit file looks like this:

#!/bin/sh

REPOS="$1"
REV="$2"

svn update /var/www/dev/public

I need it to basically do this (but with correct syntax, obviously)

#!/bin/sh

REPOS="$1"
REV="$2"

svn update /var/www/dev/public
if (pushLive.txt has changed) {
  svn update /var/www/live/public
}

Anybody have any suggestions?

2

There are 2 answers

7
CrayonViolent On BEST ANSWER

not sure if this is the most efficient way, and also this the first case will technically work on a file called "pushLive.txt" regardless of dir, so you might wanna play with that or make sure file is unique...

#!/bin/sh

LOOK=/usr/bin/svnlook
REPOS="$1"
REV="$2"

for changes in `$LOOK changed $REPOS | awk '{print $1 "=" $2;}'`;
do

  idx=`expr index "$changes" =`;
  directory=${changes:$idx};
  action=${changes:0:$idx-1};

  case "$directory" in
    *pushLive.txt )
      case "$action" in
        "U" )
           svn update /var/www/dev/test/public
           ;;
      esac
      ;;
  esac
done

exit 0
3
David W. On

I wouldn't do this through a post-commit hook since an update can take a long time. That means users have to wait around for your update to complete before they get control of their workstations again.

Take a look at Jenkins.

Jenkins is a continuous build server, but you can use it even if you don't have a build. In your case, you can use it to watch the Subversion repository for the live domain. When Jenkins detects a commit upon that Subversion URL, it can spawn a build process. In your case, it would be updating the files on the live server.

If your server is the same machine running Jenkins, you could easily specify the Jenkins Working directory to be the /var/www/live/public directory. (By default, Jenkins will create the build directory under $JENKINS_HOME/jobs/<jobname>/workspace, but there's an option in Jenkins where you can specify it). Then, there's no programming involved at all. This is faster and easier than playing around with a post-commit hook.

By the way, if you take this route, I suggest you create a new update to a clean directory, then rename the directory. Otherwise, as Subversion updates your server, you'll have files from the previous and present Subversion revision at the same time. This could cause problems if someone was on the server while the update is occurring.