Sunday, January 29, 2012

Get Latest from TFS and Build project through batch file

Recently, We had requirement where Testing team required daily build of project for Testing. So I decided to automate the process with help of batch file.

Approach for making build process automated is to use Team Foundation Build Service. Team Foundation Build Service is a Windows service that is listed in different locations within the operating system. Where it is listed depends on whether Team Foundation Build Service is running as a Windows service or an interactive service.

For using Team Foundation build service, you need to learn about the build service, agents, and controllers. 
- One needs to configure and manage Team Foundation Build Service to enable your team to automatically and consistently build, test, and deploy your software in a distributed environment. 
- Then need to install the build service and enable it to build projects that are under version control in team project. 
- Then create and manage build controllers, which handle requests for queued builds, and build agents, which handle the work of building, testing, and deploying your application. 

So overall approach is useful if you are developing Product with large code base and have large teams for developing different modules. Complete understanding of Team Foundation Build System on MSDN is available at http://msdn.microsoft.com/library/dd793166%28VS.100%29.aspx

But we require something simple for our small project which creates Build for Testing Team as well as for release and I decided to make a batch file which
- Takes Latest from TFS
- Prepare build of project with MSBuild command
- Logs the error while building project
- Sends the email for success / failure.
- Then configured to run created batch file daily through Windows Task Schedular.

Batch file works in below way:

- sets the enviornment variables so that Commands which can be run on VS Command prompt can also run from batch file with below command:
call "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat" (Set path based on installed VS Version)
- Get latest from TFS (Directory must be mapped with TFS)
- Get Latest operation is done through tf get command
tf get "%Solutiondir%" /force /recursive /login:Installshield,SHSH@kk5
- prepare build through msbuild command
msbuild "%Solutiondir%\WinTestApp.sln" /t:rebuild /p:Configuration=Release;Platform="x86";DefineConstants="SSWind SSAdmin SSNKDTT SSCompany10";OutDir=bin\BuildOutput\ /fileLogger /flp:logfile="%ErrLogFilePath%";errorsonly
- msbuild command sets Configuration, Platform and conditional compilation symbols and logs the Error occured during build process in text file
- copies all dlls / exe to specified destination directory (folder name created with DateTime format)
- Email is send to specified sender with successful or failure message
- Email send with Error Log as attachment
- Email send through Blat utility(Blat is a Win32 command line utility that sends eMail using SMTP)

Points to take care:
- Change the path of Project Directory and desitnation folder and also set the Email Variables (From, to, user, pwd)
- Spaces should be avaoided in name of folders as spaces may create problem in batch files
- Blat official website for downloading exe and for sample examples: http://www.blat.net/
- For using Blat to send mail, download the files and copy blat.exe (and other supporting files) to some folder (say blat) and run command as below to send mail:
cd E:/tarak/blat :: move to installation folder
blat %tempmail% -to %tomail% -f %frommail% -server %serveradd% -subject "Build Exe Failed" -u %mailuser% -pw %mailpwd% -attacht D:\Projects\BuildErr\errors.txt

Batch file can be downloaded from below URL:
https://docs.google.com/open?id=0B0L2UE58PUp8YWUyNDU5NDItOTcyMC00NGVhLWEzODItMzcwZTA5YjRhYWMx

You can always send comments for any suggestions or any assistance for further enhancement.

Happy Programming !!!

No comments:

Post a Comment