Simple daily build script for Visual Studio

I spent about an hour the other afternoon making a simple daily build script for Windows using Visual Studio.  It’s job is to take the source from SVN, clean it, compile it and then email the output to the programming team.  The steps needed to create and run this script are outlined below:

  1. Firstly get a hold of Visual Studio (2005 Express Edition is free), SVN for Windows and Blat (a command line e-mail client).
  2. Now open up notepad or whatever you use to create bat scripts.  The first thing we need to do with this script is to set up the Visual Studio environment.  Luckily there’s already a script out there that can do this.  Open up $VIS_HOME\Common7\Tools\vsvars32.bat and copy it’s contents into a new file.
  3. We’ve now set up our environment so we need to check out the source code.  I run the daily build from a different location to the main source tree, so change directory to the root of the source tree.  Now run
    svn update > svn.log .
    This pipes the output of the svn update into a log file which we may later need to refer back to.
  4. Now we need to build the source.  We do this in three steps using the devenv command:
    devenv path\to\solution\solution.sln /Clean
    devenv path\to\solution\solution.sln /Build Release > errors.log
    devenv /Command File.Exit

    Firstly this cleans any previous build we’ve done of this solution, then compiles it in release mode piping all output to the errors.log file. The final stage is to issue the file exit command (if we don’t do this then visual studio would be left open!).
  5. Now that we’ve built the solution and have our two log files we need to email them using Blat.  This is done really simply as follows:
    :: GET THE CURRENT DATE (LATER USED IN EMAIL AND BACKUP)
    FOR %%A IN (%Date%) DO SET Today=%%A
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    :: SEND THE EMAIL, ATTACHING THE SVN LOG AND ERROR LOG FILES
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    @echo off
    :::::::::::::: Lets set some variables ::::::::::::::
    set eMail="programmers@domain.com"
    set fromMail=dailybuild@domain.com
    set subj=-s "Build Results For %Today%"
    set server=-server your.smtp.server.com
    set body=-body "Build output from %Today%"
    set attachment=-attacht "errors.log, svn.log"
    ::::::::::::::::: Now we run Blat! :::::::::::::::::
    blat %0 -to %eMail% -f %fromMail% %subj% %body% %server% %attachment%

    The first line sets today’s date (later used in the email). Then we set up the variables (eMail is a comma seperated list of recipients) and finally we issue the blat command with the given variables.

That’s all there is to it.  There are a few ways of enhancing it (which I’ve used in work but haven’t written about), maybe when I get time I’ll post them up too.


About this entry