Go to content Go to navigation Go to search


TFS BuildStore: Stopping and removing builds
· 2008-04-11 12:17 by Thijs Kroesbergen for Brokenwire.NET

Here is a quick tip on how to remove a build that got stuck in your Team Foundation Server build environment.

The scenario: You've started a build and someone else has decided that it's a good idea to shutdown the buildserver... (Hi Martijn!)
After that you're left with a started build in the TFS buildstore with a BuildStatus of "Compilation Started". The problem here is that this build will never finish (because the server was shutdown while building). As an additional bonus you'll have team explorer sorting this build to the top of the list as well, which is confusing. On top of that: the list of builds doesn't have a button to delete a build.

So we want to remove it. How? PowerShell!

I used my friend get-tfs, and added a line to the script for the BuildController object. The complete get-tfs function now looks like this:

function get-tfs ( [string] $serverName = $(Throw 'serverName is required') ) { # load the required dll [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client") $propertiesToAdd = ( ('VCS', 'Microsoft.TeamFoundation.VersionControl.Client', 'Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer'), ('WIT', 'Microsoft.TeamFoundation.WorkItemTracking.Client', 'Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore'), ('BS', 'Microsoft.TeamFoundation.Build.Common', 'Microsoft.TeamFoundation.Build.Proxy.BuildStore'), ('BC', 'Microsoft.TeamFoundation.Build.Common', 'Microsoft.TeamFoundation.Build.Proxy.BuildController'), ('CSS', 'Microsoft.TeamFoundation', 'Microsoft.TeamFoundation.Server.ICommonStructureService'), ('GSS', 'Microsoft.TeamFoundation', 'Microsoft.TeamFoundation.Server.IGroupSecurityService') ) # fetch the TFS instance, but add some useful properties to make life easier # Make sure to "promote" it to a psobject now to make later modification easier [psobject] $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($serverName) foreach ($entry in $propertiesToAdd) { $scriptBlock = ' [System.Reflection.Assembly]::LoadWithPartialName("{0}") > $null $this.GetService([{1}]) ' -f $entry[1],$entry[2] $tfs | add-member scriptproperty $entry[0] $ExecutionContext.InvokeCommand.NewScriptBlock($scriptBlock) } return $tfs }

Next I had to figure out the BuildUri for the broken build. I used the following snippet to get all builds with a status of "Compilation Started" from the BuildStore:

$tfs = get-tfs http://tfs-server:8080 $tfs.BS.GetListOfBuilds("TeamProject", "DailyBuild") | where {$_.Buildstatus -eq "Compilation Started"}

Before running this, I made sure no other builds where running and to make sure you are going to delete the correct build you MUST double check the results here! Next I copied the BuildUri from the build that I wanted to stop & delete.

Then the following three lines of PowerShell made the magic happen:

$message = "" $tfs.BC.StopBuild("vstfs:///Build/Build/04102008_134057_94286", [ref] $message) $tfs.BC.DeleteBuild("vstfs:///Build/Build/04102008_134057_94286", [ref] $message)

If something goes wrong the $message variable will be filled with a helpful error message.

By combining the BuildStore and the BuildController objects it is also possible to write an automated build-cleanup mechanism using PowerShell. (TFS2008 has this functionality built-in, but version 2005 doesn't)
The pseudo code for this script would look like this:

Loop through ListOfBuilds WHERE BuildQuality equals "Rejected" (maybe another Quality?)
And for each build found:

Writing this in PowerShell should be easy now. And as usual: the actual implementation of this script is left as an exercise for my dear readers ;)

Let me know if you succeed (or fail, in that case we'll sort it out together).

Permalink -

  1. This worked great, thanks!

    Ruben    2008-05-02 08:40    #