Subversion as build tool
I use Subversion to manage all of my projects, since it’s an easy tool to setup, and manage repositories with. I use the SmartSVN client to manage all my check-ins and checkouts. But something I have with phpVMS to to automatically create builds with the proper version numbers. I have an update script, which will make database changes, as well as update the version numbers. I base the version number off of the Subversion revision number, which is incremented every time code is checked in. The steps I take for a build are:
- Check the current revision number
- Update the update script to the next revision number
- Commit the check in
- Export the SVN code
- Create a tar file with the full install, with the build number
- Create another update install, with the local config removed, with the build number
- Move these files to the download area
- Move the exported code into my test area to keep it updated.
It’s annoying to do with each check in, and whenever there’s an update, I have to do it again. This is the perfect flow to create a script to handle. So I created a bash script, which can be called through a cron-job, on the command-line, or as a post-commit hook in the repository. So here’s the script (read through the comments!)
[bash]
#!/bin/bash
# Variables
REPOS="http://svn.devjavu.com/phpvms/trunk"
EXPORT="/path/to/temp/export"
MOVETO="/path/to/test/directory"
DOWNLOADSPATH="/path/to/downloads/directory"
# Revision Number will be tacked on the end
VERSION="1.0."
# Cleanup our export directory
rm -drf $EXPORT
# Export and get the revision number
echo "Exporting from SVN"
REVISION=`svn export –force $REPOS $EXPORT | grep -i -o -e "revision.*[0-9]"`
REVISION=${REVISION:9}
echo "Retrived revision $REVISION, adding to update file"
# Add the version number into the update script
# Replace the <<UPDATE>> with the version + revision number
sed -i "s/<<UPDATE>>/$VERSION$REVISION/g" $EXPORT/install/update.php
# Create the full install
echo "Creating full install file"
rm -f $EXPORT.$REVISION.full.tgz
tar czf "$EXPORT.$REVISION.full.tgz" $EXPORT
# Create the update
# Remove the local.config.php file
echo "Creating the update"
rm -f $EXPORT.$REVISION.update.tgz
rm -f $EXPORT/core/local.config.php
tar czf "$EXPORT.$REVISION.update.tgz" $EXPORT
# Clean up the destination directory
# But I don’t want to since my config file is there
#if [ -d $MOVETO ]; then
# rm -drf $MOVETO
#fi
#mkdir $MOVETO
echo "Moving to test area"
# Now move it to our test area
cp -Rf $EXPORT/* $MOVETO
# Move our tars to our download area
echo "Moving downloads"
mv -f $EXPORT.$REVISION* $DOWNLOADSPATH
echo "Done!"
[/bash]
So I do an export of the SVN cache, and then run “grep” to get the revision number. After that, it’s just basic directory and file manipulation. It’s also helpful if you need to compile some code.
In my update.php, I placed the following:
[php]
define(‘UPDATE_VERSION’, ‘<<UPDATE>>’);
[/php]
Which then gets replaced by the version number with this line:
[bash]
sed -i "s/<<UPDATE>>/$VERSION$REVISION/g" $EXPORT/install/update.php
[/bash]
If you have multiple files, it’s just a matter of copy/pasting that line to include your files with the update. Enjoy!
Hey dude, this is nice!!!
[...] to move all my shell scripts, which I used to build and deploy some of my applications (outlined in this post). I was thinking there had to be a better way, after all, Ruby has Capistrano, and though it can be [...]