TFS, Team Build and a Build Confirmation 0

We recently wanted to add a manual verification to our TFS builds. We didn’t want someone accidentally clicking the build to production when they meant to build to development. They are right next to one another and look very similar.

I didn’t want to spend a lot of time on it, so I came up with this. It’ll work for now and ensure we manually interact with the queue build dialog.

<Target Name="BeforeEndToEndIteration" Condition="'$(C***)' != 'suck'" >
    <Error Text=
"You did not state your feelings for the C***. Please state this by submitting
 the parameter /p:C***='suck' in the queue build dialog."/>
</Target>

The parameter name realy isn’t “C***”, but I didn’t want to offend any followers of the team with the longest championship drought of any professional sports team. Actually, I bet it is safe to extend that to individual performers too.

Update on Beyond Compare inside Team Build and why I’m starting to hate TFS 7

In the past few days, and nights, I’ve learned a few things. Most are bummers, but there is light at the end of the tunnel. This is what I feel our build process was like for the past few days, but I think we’re doing much better now and things are actually working. (Cross your fingers)

I’ve tried a few approaches yesterday and here is what I’ve found.

Deploying via Ftp

Initially, I tried to deploy to our servers via FTP. This appears to work well, but the timestamps are all adjusted by the GMT offset. In our case, the files end up on the server looking like they were edited 6 hours in the future. This makes every single file appear different on the next sync since none of the time stamps match. Apparently this is by design from Microsoft. What a bummer. Running the development server on GMT will cause the timestamps in the database on that server to also be off. This option is out.

Deploying via file share

Again, this appears to work well. The first time everything goes. We change a single file and the log sees that only one file has changed, but doesn’t move it. Doh! They come out of TFS as read-only, and copying to a file share will carry over those file attributes. No problem.  

attrib -r \\myServer\myShare\myApp\*.*

Nope! You can’t run attrib on a network share. It has to be a physical disk.

OK. Well I’ll just attrib -r the source files in the build’s working directory. Great! That worked. Let’s try it again. Doh!

Unable to perform the get operation because it is writable.

Miedo

You’re kidding me! Grr…OK! Fine! I’ll attrib +r after I push. Nope! Not every file in the source directory is suppose to be read only and some of them cannot be read only. AHHH!

Oh yes! Beyond Compare has an attrib -r command in the scripting reference.

ATTRIB
Usage: attrib (+|-) [(+|-) <...>]

Sets (+) or clears (-) the DOS file attributes in the current selection. An attribute set can include archive (a), system (s), hidden (h), and read only (r) attributes. Windows only implementation: Linux version will not recognize ATTRIB.

Sound great right?….that doesn’t work. Even on simple unit test this doesn’t work. I’m at my wit’s end now. There is one more thing to try. There is one more command in the Beyond Compare script reference that might be able to help.

OPTION
Usage:
option stop-on-error
option confirm:(prompt|yes-to-all|no-to-all)

Adjusts script processing options.
· stop-on-error makes the script watch for various error conditions, including file operation errors, and, when one occurs, prompts the user before continuing.
· confirm can use prompt, yes-to-all, or no-to-all to handle confirmation dialogs that occur due to file operations. By default, prompt is used.

Let’s hope this works. I’ll put it right before the sync, cross our fingers and run it through some tests.

It WORKS! Or it appears to so far. Hopefully.

The files on the server still carry the read only flag. That’s not ideal for us, but it will have to do for now.

TFS, Team Build, and Beyond Compare as a lighter XCopy 5

We’re using TFS 2008 and Team Build to compile and build a website mixed with ASP.Net and classic ASP. It works fine, but the only problem is the site is huge. It’s over over 2500 files and over 400 megs. That’s HUGE! We could XCopy that to our server, but pushing 400 megs every time feels like a little bit of overkill, especially when the majority of the changes are copy updates. We could also use a content management system for this, but we thought we would give try it with TFS.

First, it took forever to pull 400 megs out of source control every time we did a build. We can fix that.

<PropertyGroup>
    <IncrementalGet>true</IncrementalGet>
<PropertyGroup>

Done. Now the build will only pull down the files that changed. It’ll leave the files from previous build laying around on the server, but that’s OK.

Next, it takes forever to copy the 400 megs to the drop folder. We don’t even need to do it in the first place since we are going to xcopy the _PublishedWebsites contents elsewhere when we’re done. We can fix this problem with ease too.

<PropertyGroup>
    <SkipDropBuild>true</SkipDropBuild>
<PropertyGroup>

Now we won’t copy the build to the drop server.

So finally the XCopy. That was a little more tricky. We need to figure out only the files that change and transfer them, via FTP in out case. Luckily, Beyond Compare not only has a scripting interface that will let us work it into the build script, it also has the example script of how to do exactly what we want to do in the documentation. Win for us!

We can just tweak it a bit and we’re off to the races. Let’s make a script file called deploy.to.server.script and put it in the same folder as the TFSBuild.proj.

# %1 is source of the build to copy to the new server
# %2 is the outdir where the log should be written

#log the script actions
log verbose "%2\deployment.log.txt"

#set the comparison criteria
criteria timestamp size

#load source and target
# you could also use a UNC share
load "%1" "ftp://user:password@oursite.com"

# Move everything
filter "*.*"

#Sync the local files to the web site, creating empty folders
sync create-empty mirror:lt->rt

Next, invoke it in the build script.

<Exec
 Command="&quot;c:\Program Files\Beyond Compare 3\bcompare.exe"
 &quot;@$(MSBuildProjectDirectory)\deploy.to.server.script&quot;
&quot;$(SolutionRoot)\MyWebsite" &quot;$(OutDir)&quot;  \silent \closescript" >

We’re done. It’ll only copy the stuff we’ve changed and we’ve taken our build time from 25 minutes down to less than one.

Update: If you want to keep an eye on what you’re moving, add the following steps in the Beyond Compare script.

#load source and target
# you could also use a UNC share
load "%1" "ftp://user:password@oursite.com"

expand all
folder-report &
    layout:side-by-side &
    options:display-mismatches &
    title:"Deployment Report" &
    output-to:"%2\deployment.summary.html" &
    output-options:html-color

Sorry about the funny syntax highlighting. There is not “BeyondCompare” language available.

You’ll get a nice HTML report.
ss-20090129092919

Another Update: It looks like Beyond Compare will not tell you if an error occurs during the script. This is a huge bummer. If you get something like this in your script, it’ll still return 0 for the ExitCode and you won’t know it failed.

1/29/2009 11:40:42 AM  Fatal Scripting Error: Unable to load base folder
1/29/2009 11:40:42 AM  Script completed in 0.01 seconds

Yet another update: There is some new news on using Beyond Compare with Team Build.

One more update: Here is a copy of the edit to the TFSBuild.proj file. This gets added at the end of the file before the closing <Project> tag.

 	<PropertyGroup>
		<IncrementalGet>true</IncrementalGet>
	</PropertyGroup>

	<!-- Since we are doing the source side by side we deploy before
		copying to the the builds folder. We might consider changing this
		to AfterDropBuild and copy from there if we are doing the precompiled
		websites -->
	<Target Name="BeforeDropBuild">
		<CallTarget Targets="Deploy"/>
	</Target>

	<Target Name="Deploy">
		<!-- Create a Custom Build Step -->
		<BuildStep
			TeamFoundationServerUrl="$(TeamFoundationServerUrl)"
			BuildUri="$(BuildUri)"
			Name="BeyondCompareDeployStep"
			Message="Deploying to webserver">
			<Output
				TaskParameter="Id"
				PropertyName="BeyondCompareDeployStepID" />
		</BuildStep>

		<!-- We are deploying the source side by side with the pages instead of
			doing the precompiled website-->
		<Exec Command="&quot;c:\Program Files\Beyond Compare 3\bcompare.exe&quot; &quot;@$(MSBuildProjectDirectory)\deploy.to.server.script" &quot;$(SolutionRoot)\source&quot;  &quot;\\myWebserver\path\to\mysite&quot;  &quot;$(DropLocation)\$(BuildNumber)&quot;  \silent \closescript" >
			<Output
				TaskParameter="ExitCode"
				PropertyName="BeyondCompareExitCode" />
		</Exec>

		<!-- These tests don't work as BeyondCompare will ALWAYS exit with code 0 from script -->
		<BuildStep
			Condition="'$(BeyondCompareExitCode)' == '0'"
			TeamFoundationServerUrl="$(TeamFoundationServerUrl)"
			BuildUri="$(BuildUri)"
			Id="$(BeyondCompareDeployStepID)"
			Status="Succeeded"/>
		<BuildStep
			Condition="'$(BeyondCompareExitCode)' != '0'"
			TeamFoundationServerUrl="$(TeamFoundationServerUrl)"
			BuildUri="$(BuildUri)"
			Id="$(BeyondCompareDeployStepID)"
			Status="Failed"/>
		<Error
			Condition="'$(BeyondCompareExitCode)'!='0'"
			Text="Deployment Failed" />
	</Target>

The olny other file is the deploy.to.server.script file. This file sits aside the TFSBuild.proj file and looks like this.

# %1 is the solution root of the build
# %2 is the target location (ftp or share) to deploy
# %3 is the outdir where the log should be written

#log the script
log verbose "%3\deployment.log.txt"

#set the comparison criteria
criteria timestamp size

#load source and target
load "%1"  "%2"

expand all
folder-report &
    layout:side-by-side &
    options:display-mismatches &
    title:"Deployment Report" &
    output-to:"%3\deployment.summary.html" &
    output-options:html-color

filter "*.*"

# we want to override all the read-only files that exist
option confirm:yes-to-all

#Sync the local files to the web site, creating empty folders
sync create-empty mirror:lt->rt

Automating the Build (Step 1 cont’d) 1

After the disappointing ending to my previous post, I now have much success to report. Once we had the correct path to the MSTest.exe on the build server, I was able to publish the results of my NUnit tests. There are a few things to note.

  1. You still need MSTest.exe on the build server. I guess that is OK since we, at our organization, want to allow for the ability to run MSTests as well. I’m not 100% sure that happens on the build server or the TFS server.
  2. As of now, if the NUnit tests fail, it will not run the MSTest. This is because we are executing the NUnit tests in the AfterCompile target as suggested in the NUnit for Team Build example. I’m not really sure why you would do both in the same solution, but if you do, you won’t see failures from both. Only one at a time. I’m looking into getting both to run, but that isn’t going to be much of a priority since we won’t need to run both in the same solution.

I am really excited that we have this running with such little effort. Thanks has to go to the NUnit for Team Build guys.

One to step two when time allows.

Automating the Build (Step 1) 2

We are starting to automate and standardize our build process. I think the idea that F5 is not a a build strategy is finally surfacing as a real problem. The first step in my mind is to come to some convention on the structure. Doing so would make configuration a lot simpler. We can default the convention and you can overwrite if you feel the need.

Microsoft has some guidance on the subject of how to structure the source when using TFS. I’m not really sure how the choice of source control factors into that decision, but we’ll just go with it. The structure I’m playing with now is very similar to their recommendation.

/Fooberry/
          1.0.0/  <-- the build file goes here
                docs/
                source/
                       application/ <-- the source for the ui
                       application.tests/ <-- the UI tests
                       core/
                       core.tests/
                       ...I'm not settled on how this level
                          will look. It isn't important for
                          now. I'm more concerned with the
                          files needed to build.
                libs/
                    my.company.build/
                    msbuild.community.tasks/
                    nunit/
                    nxslt/
                    nunit4teambuild/
                    n*...all shared libs and tools
               ...maybe some other dirs here eventually
          main/ <-- the trunk copy just like the 1.0.0 tree
/SomeOtherApp/

The main point here is that all the tools we use are inside the source control for every app and every branch of that app. It may seem like a waste of disk space, but what it allows is all configuration information to be relative to the build root. A specific branch can upgrade to a new NUnit and all the other builds don’t need to worry about backwards computability.

A folder that may look a little odd is my.company.build. I would like this folder to hold as much of the automated build extensions as possible. There are going to be a few additional steps that we need to add to the out of the box TFSBuild.proj. The plan is to put all the new targets in a my.company.build.targets file along with any custom tasks that we need to build. Once we have that, we can stick the following line into the TFSBuild.proj and we snap on all our custom steps.

<Import Project="$(SolutionRoot)\libs\my.company\my.company.build.targets"/>

Getting the import setup was a little bit of pain. Running locally, $(SolutionRoot) doesn’t map to the correct path. The path to the targets file was mapping someplace crazy. This has a really bad smell, but I was overjoyed that it worked remotely, so I didn’t care too much for now. It’s only day one. We will need to build locally eventually. That’s on the TODO list already.

I have a bit of a confession. I wish that was the manner in which I started this process, but I didn’t realize that we could externalize the build steps to my.company.build until after I started filling out the steps. This is is how, in my opinion, frameworks are most successfully built. We extract the code that could be made common instead of head out to write something that will solve everyone’s problem, even though you don’t know what their problem might be.

So to start on the first build step; running NUnit tests. After a quick Google search,
Nunit4TeamBuild on CodePlex outlined the steps we needed to take.

  1. Run Nunit-console and produce an XML log file
  2. Convert the Nunit XML output to an MSTest test results file (with a .trx extension)
  3. Use MSTest /publish to push merge the trx file with an existing build

Sounds pretty straight forward. There are a few more dependancies. It uses the MSBuild Community Tasks and nxslt2 (There is a new nxslt3 which uses .Net 3.5…not sure what that gains us though).

The documentation for Nunit4TeamBuild recommends adding an AfterCompile target to execute the NUnit tests. We can put this in our my.company.build.targets file. There are just a few housekeeping items first. The example proj file has hard coded paths to the MSBuild Commnuity Tasks, NUnit and NXslt. To make those relative, assign the….

<PropertyGroup>
   <MSBuildCommunityTasksPath>$(SolutionRoot)\libs\community.msbuild.tasks\</MSBuildCommunityTasksPath>
   <NUnitPath>$(SolutionRoot)\libs\nunit\</NUnitPath>
</PropertyGroup>

<Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.Targets"/>

When we go to invoke the NUnit task, we need to be sure and give the ToolPath of $(NUnitPath).

<NUnit ContinueOnError   = "true'
        Assemblies       = "@(TestAssemblies)"
        OutputXmlFile    = "$(OutDir)nunit_results.xml"
        ToolPath         = "$(NUnitPath)">
   <Output TaskParameter = "ExitCode"
           PropertyName  = "NUnitResult" />
 </NUnit>

*Note: I’m playing with the formatting of the XML. I’m trying out this style for now.

So that takes care of the NUnit and the MSBuild Community Tasks. The next tool that needs a relative path is NXslt. The example project shows invoking the command line executable via the Exec task.

<Exec Command="C:\path\to\nxslt\nxslt2.exe &quot;$(OutDir)nunit_results.xml&quot; &quot;$(SolutionRoot)\nunit transform.xslt&quot; -o &quot;$(OutDir)nunit_results.trx&quot;"/>

While that works, NXslt comes bundled with an MSBuild task that can replace it with something more elegant. To use the task, add the Using

 <UsingTask AssemblyFile = "$(NXsltTaskAssembly)"
            TaskName     = "Nxslt" />

And replace that Exec with the following:

<Nxslt In    = "$(OutDir)nunit_results.xml"
         Style = "$(SolutionRoot)\libs\nunit4teambuild\nunit transform.xslt"
         Out   = "$(OutDir)nunit_results.trx" />

One more to go. MSTest.exe is invoked to publish the newly formatted results. This one might be more difficult to keep relative. Right now, this is pointing (or trying to point) to the MSTest.exe under Program Files. With all that said, I’m unable to find where MSTest is on the build server. Having no access to it makes it a little difficult. Unfortunately I cannot guarantee that the final step will actually work. I hope to have more news to report next week.