TeamBuild: Scheduled build using Team System

Sorry to all the folks who read my blog for C#. This ones about Team System and specifically about the Team Build module :)

Team Build does not have any in-built scheduler and so it does not provide scheduled builds out of the box. However, it does have a command line tool to start team-builds. Any scheduler like the Windows Task Scheduler can be easily setup to use this command line Team Build utility to start builds at predetermined time. The Task Scheduler can be setup using a simple batch file or manually using the Add Scheduled task wizard that comes with Windows.

Where do I setup the scheduler?
Since the scheduler will use the command line TfsBuild.exe utility, you'll need to start the scheduler on either any one of the TFS clients or on a Build Machine.

Doing it through the Scheduled Task Wizard
To open Scheduled Tasks on Windows XP or Windows 2003, click Start, click All Programs, point to Accessories, point to System Tools, click Scheduled Tasks and then Add Scheduled Task. Then choose the TfsBuild.exe (ProgramFiles\Microsoft Visual Studio 8\Common7\IDE\TfsBuild.exe) as the executable and set the schedule and the account information under which you want to run the TfsBuild. At the last page ensure that the "Open advanced properties..." is checked before you hit finish...

In the dialog that opens enter the arguments in the Run text box so that its like...

"d:\Program Files\Microsoft Visual Studio 8\Common7\IDE\TfsBuild.exe" start <TfsServer> <Team Project> <Build Type name>

Doing it through a batch file
I wrote a simple batch file that makes this even easier. Copy/paste the following into a file named schbuild.bat and place it in %ProgramFiles%\Microsoft Visual Studio 8\Common7\IDE and run schbuild.bat to see usage.

 @echo off

if "%1" == "" goto Help
if /i "%1" == "-h" goto Help

set server=%1
set project=%2
set buildType=%3

set schedule=%4
set startTime=%5
set startDate=%6

set schName=%2_%3

set tfsCommand="D:\PROGRA~1\MID05A~1\Common7\IDE\TfsBuild.exe start %server% %project% %buildType%"

set user=%userdomain%\%username%
echo %tfsCommand%

schtasks /create /sc %schedule% /tn %schName% /tr %tfsCommand% /st %startTime% /sd %startDate% /ru %user% /rp *
Goto Done

:Help
@echo Usage: schbuild TeamfoundationServer TeamProject BuildType Schedule StartTime StartDate
@echo.
@echo     TeamfoundationServer   The Team Foundation server.
@echo.
@echo     TeamProject            Team project name.
@echo.
@echo     BuildType              Build Type name.
@echo.
@echo     Schedule               Specifies the schedule frequency. 
@echo                            Valid schedule types: MINUTE, HOURLY,
@echo                            DAILY, WEEKLY, MONTHLY, ONCE.
@echo.
@echo     StartTime              Time to start the build in HH:MM:SS 
@echo                            (24 hour format)
@echo.
@echo     StartDate              Specifies the first date on which the
@echo                            task runs. The format is mm/dd/yyyy.

:Done

Sample batch file run
To start a nightly build that happens every night at 9:00p.m. use something like

SchBuild.bat MyTfsServer MyTeamProject NightlyBuildType DAILY 21:00:00 11/21/2205

Customizing the Schedule
Once the Team Build has been scheduled through the batch file it can be easily modified using the scheduled tasks user interface. To open Scheduled Tasks use the same steps as mentioned aboveĀ and then double click on the task named <Team project>_<BuildType> to open it and modify as required.