I recently decided to outsource my souce code management to CVSDude. I had been running SVN on my VPS that I have at Slicehost and that had been running okay. However, I’m only good enough at SVN administration to get the server up and running and create a few repositories and there were other things that I needed to do with SVN that I frankly didn’t have time to learn how to do.
One of the things that I’d heard talked about that made life much easier was using Subversion’s various hooks to automate some things around different SVN events. I emailed tech support asking how we could configure the “post-commit” hook to update the files on my VPS each time I made a commit into the SVN module. The tech told me that they do this by configuring an HTTP call to a page (or script etc) on your server. That page then triggers an SVN update of the specific module.
After hearing this, I began thinking about how to use CFEXECUTE to trigger the SVN update and was actually surprised how simple it was. My VPS runs on Fedora Core 6, so, like any *nix operating system, you need to make sure permissions on the files are set so that the user which runs ColdFusion can write to the directories. Once that was accomplished, all it took was a few lines of code and we’re ready to start receiving HTTP callbacks from CVSDude’s servers.
CVSDude structures their SVN URLs in the format http://username-svn.cvsdude.com/modulename (or https://). All my modules are named in reverse domain order for the web site that it is for (for example, this one is ws.skaggsfamily.dan). I had already checked out all my modules into a specific directory.
The HTTP callback from CFSDude’s server has 4 URL parameters that they send over: root (full path to module), author (user), revision and date. So all I really had to do was grab the URL.root value and extract the name of the module, pass that to the CFEXECUTE tag and, voila, automatic updating of the module immediately after the commit.
There were only 2 gotchas that I had to figure out:
- Put the full path to the svn executable
- Add in your username and password to the string in the arguments attribute of CFEXECUTE
The code that I used is below. I’m sure there’s more that I could have done with it, but this is a good start I think.
<cfif structKeyExists(URL, “date”) AND URL.date NEQ “” AND structKeyExists(URL,”author”) AND URL.author NEQ “” AND structKeyExists(URL, “revision”) AND URL.revision NEQ “” AND structKeyExists(URL, “root”) AND URL.root NEQ “”> <cfset module = replaceNoCase(URL.root, “http://username-svn.cvsdude.com/”, “”)> <cfexecute name=”/usr/bin/svn” arguments=”up /var/sites/#module# –username yourUserName –password yourPassword” timeout=”300″ variable=”result”> </cfexecute> <cflog log=”Application” type=”information” text=”#URL.author#:#URL.root#:#URL.revision# – #result#”> <cfmail to=”email@example.com” from=”firstname.lastname@example.org” subject=”Server updated successfully”> The server has been successfully updated with your lastest changes. Details of the update are below: Date: #URL.date# Module: #URL.root# Revision: #URL.revision# Results: #result# </cfmail> </cfif>