Wednesday, June 20, 2012

How To Set automated Build using Custom “.proj” file and Upgrad Template in TFS 2010

How To Set automated Build using Custom “.proj” file and Upgrad Template in TFS 2010

Below I am trying to give simple and full steps to create automated build for any .NET application using TFS 2010. So here we go
Prerequisite Steps
  • To do automated build, I believe you should have one separate Build machine and this build machine should have all admin permission to connect to your VSTS server. 
  • Install all prerequisite required for your application.
  •  Make sue build machine has Microsoft Team Foundation Server 2010 Installed
Once you have all the setup or installation done then you can proceed to create automated build for your application.

Below I am setting up automated build for one of the Silverlight Project.

Step -1
To start build configuration, very first step to setup Controller and Agent for the build
Open Team Foundation Administrator Console

Steps-2
Go to the “Build Configuration”

Steps-3
Click on the Build controller properties and Create the Build controller by giving Display name (ex: Project name - Controller) , click Test Connection and click OK
Steps-4
Create the Build Agent by giving Display name and select previous created controller (Steps-3), define Working Directory path, you can add new tag if any and say OK
Same way you can create multiple agent for single controller now you can close Build Configuration.
Steps-5
After you done with Controller and Agent setting, you can go head and create build definition for your project. To do that Open visual Studio and Team Explorer window to create builds definition
Right click and select “New Build Definition” on the Team Explorer window

Steps-6
Type the Build Definition name and description as per your project

Steps-7
Go to the new tab Trigger on the same window and select the build option as you want
Here you can set your daily build setup like every day midnight your application get build, you can specify day and time too

Steps-8
Go to the new tab Workspace on the same window and define the “Source Control Folder” (ex: $/VSSRootFolder) and “Build Agent Folder” (by default:  $(SourceDir))

Source Control Folder: select the folder where all your project or solutions files exists
Steps-9
Go to the new tab Build Defaults on the same window and select the controller as you’re created on Steps-3 and define the folder path where you want the output file from this build

Steps-10
Go to the new tab Process on the same window


Go to the Build Process template and select “Upgrade Template”
This option we have to select if we want to build all the projects in different folder structure, The Default template will create all projects output into one single folder

Now on the Build Process parameters
Give the TFSBuild.proj file path into “Required -> Configuration Folder Path
On the “Advancedà MSBuildPlatform” select X86 


Save your build definition
Steps-11
“.proj” file should be on the “TeamBuildTypes” folder as same shown on the above diagram.

Now Open TFSBuild.proj files into visual studio and mention the project to build in proper order
Set the build configuration

 <ItemGroup>
    <ConfigurationToBuild Include="Release|AnyCPU">
      <FlavorToBuild>ReleaseFlavorToBuild>
      <PlatformToBuild>AnyCPUPlatformToBuild>
    ConfigurationToBuild>
ItemGroup>

Mention the project name you want to build and mention the folder name where you want build output files.

<ItemGroup>
<SolutionToBuild Include="$(BuildProjectFolderPath)/../../ProjectRootFolder/Dev/First.csproj">
       <Targets>Targets>
  <Properties>OutDir=$(BinariesRoot)\$(Configuration)\OutFolderName\;OutputPath=$(BinariesRoot)\$(Configuration)\OutFolderName\;Properties>
       SolutionToBuild>
ItemGroup>

$(BuildProjectFolderPath): This is the path you mentioned on the steps-8 “Source Control Folder”
$(BinariesRoot)\$(Configuration)\: This is the path you mentioned on the steps-9 (\\ServerName\Ritesh\)
OutFolderName: This is the folder where your First.csproj file output stored after build.
//ServerName : Build machine name
So finally “First.csproj” output will be stored into “//ServerName/Ritesh/OutFolderName/”, all Dll’s, bin folder and config files will be stored into this folder after build.
Call another "Proj" from first "Proj" file
Little advanced , You can call another msbuild file from proj file. Your msbuild file should be on the same folder where your proj file exists
So your “.msbuild” file should be as below folder structure
Project RootFolder à TeamBuildTypes à ProjectBuild à Build_Database.msbuild
Once you have “.msbuild” file ready you can specify new ItemGroup into “.proj” file like below.
 
<ItemGroup>
<SolutionToBuild Include="$(BuildProjectFolderPath)/../Build/Build_Databases.msbuild">
             <Targets>Targets>
<Properties>OutDir=$(BinariesRoot)\$(Configuration)\Release\Databases\; OutputPath=$(BinariesRoot)\$(Configuration)\Release\Databases\;Properties>
SolutionToBuild>
ItemGroup>

Let’s say you have some supporting files into the VSTS folder which should be a part of your project build and you want to copy those files when you run the build then you can separate that kinds of things into different “.msbuild” file and include into “.proj” file as per your need.
You can copy some of the folders or files from source save into your build folder like below

“Build_Databases.msbuild” 

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Run">
<PropertyGroup>
<DirectoryDatabaseRoot>$(MSBuildProjectDirectory)\..\..\SDPvNext\Dev\DatabasesDirectoryDatabaseRoot>
<DirectoryDBOperations>$(DirectoryDatabaseRoot)\OperationsDirectoryDBOperations>
<DropFolder>$(DropLocation)\$(BuildNumber)Dropfolder>
<DBNameOperations>OperationsDBNameOperations>
  PropertyGroup>

With the following line I am copying $/VSTSRootFolder/Database/Operation folder into my build location (//ServerName/Ritesh/Release/Databases/Operation)
<Target Name="CopyFiles">
<Exec Command="xcopy /y /e "$(DirectoryDBOperations)" "
$(Dropfolder)\Release\Databases\$(DBNameOperations)\""/>
Target>

<Target Name="Run">
       <CallTarget Targets="CopyFiles"/>
Target> 

Steps-12
Once your build created, you can edit the build definition or Run build definition manually, to run build manually go to the build definition, right click and clicks on  “Queue New Build” check some property like
  • Build controller : the controller you created on steps-3
  • What do you want to build :  This will tell whether you want latest code to build or some old code from VSTS
  • Set the priority and: Normal/High etc.
  • Drop folder for this build : here your output files are going to save
Clicks on “Queue” button
Build process will get start and does the following
1)      Take latest code from VSTS
2)      Build the project as mentioned in the “.proj” file
3)      Copy all build output file into build machine “Target folder” as mention in the “.proj” file
4)      Copy all the files from VSTS to target folder in the build machine  as mentioned in the “.msbuild” file
5)      Display the status of the build, Succeed or Fail

References
Understanding a Team Foundation Build System
Video: Let Build Automation in Microsoft Visual Studio Team Foundation Server 2010 Work for You

Troubleshoot

While doing the build I got lots of build errors, wanted to show here with solution
MSBuild Fail Error: The path is already mapped in workspace WORKSPACE NAME
The complete error was “error: The path C:\Builds\RiteshTest\Sources is already mapped in workspace 2113_459_MachineName”
This error occurs when you already have one Workspace defined for one Build definition and again you are trying to create new build definition on the same Workspace.
To solve this error go to the build agent and change the working directory for your new Build Definition
MSBuild Fail Error: The Silverlight 4 SDK is not installed. Using TFS 2010
The complete error was: “C:\Program Files (x86)\MSBuild\Microsoft\Silverlight\v4.0\Microsoft.Silverlight.Common.targets(104,9): error : The Silverlight 4 SDK is not installed”
I resolved this issue by changing MSBuild Platform Setting from Auto to X86 on the Build Definition Window. You need to make sure to avoid this error
·         Make sure you have Installed Silverlight 4 SDK on build server.
·         On the Build Definition window, change the MSBuild Platform from Auto to x86, as described here
MSBuild Fail Error: The type 'Silverlight.Web.User' already contains a definition for 'DisplayName'
I got this error when I was trying to make automated build for Silverlight 4.0 application using TFS 2010.
The complete error was “Generated_Code\Models\Shared\User.shared.cs (13): The type 'MSIT.ES.SDPvNext.SilverlightUX.Web.User' already contains a definition for 'DisplayName'”
I resolved this issues by changing Platform and Configuration combination on the TfsBuild.proj file previously it was “Release|x86” and on the Build Definition window, change the MSBuild Platform from Auto to x86.
TfsBuild.proj 

<ItemGroup>
    <ConfigurationToBuild Include="Release|AnyCPU">
      <FlavorToBuild>ReleaseFlavorToBuild>
      <PlatformToBuild>AnyCPUPlatformToBuild>
    ConfigurationToBuild>
ItemGroup>

No comments: