Created
January 12, 2016 19:53
-
-
Save miklund/d2c7ab3a5a6cbf02c7fd to your computer and use it in GitHub Desktop.
2013-02-08 Integrating with Team System Build
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Title: Integrating with Team System Build | |
# Author: Mikael Lundin | |
# Link: http://blog.mikaellundin.name/2013/02/08/integrating-with-team-system-build.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// get the project collection | |
var url = new Uri("http://teamserver:8080/tfs/collection"); | |
var projectCollection = Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory.GetTeamProjectCollection(url); | |
// get the build service | |
buildServer = projectCollection.GetService<IBuildServer>(); | |
var spec = buildServer.CreateBuildQueueSpec("*", "*"); | |
while (true) | |
{ | |
// fetch all queued builds | |
var queuedBuilds = buildServer.QueryQueuedBuilds(spec).QueuedBuilds; | |
foreach (var queued in queuedBuilds) | |
{ | |
if (watchList.ContainsKey(queued.Id)) | |
{ | |
// already watching this build | |
continue; | |
} | |
// start watching on another thread | |
watchList.Add(queued.Id, queued.BuildDefinition.Name); | |
var task = new Task(WatchBuild, queued); | |
task.Start(); | |
} | |
Thread.Sleep(10000); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void WatchBuild(object state) | |
{ | |
var queued = (IQueuedBuild)state; | |
Console.WriteLine("Watching: {0} - {1} ({2})", queued.TeamProject, queued.BuildDefinition.Name, queued.Status); | |
while (queued.Status == QueueStatus.Queued || queued.Status == QueueStatus.InProgress) | |
{ | |
Thread.Sleep(1000); | |
// get updated status | |
queued.Refresh(QueryOptions.All); | |
} | |
// get complete build details after build completed | |
var detail = buildServer.GetAllBuildDetails(new Uri(queued.Build.Uri.ToString())); | |
var buildErrors = InformationNodeConverters.GetBuildErrors(detail); | |
var buildWarnings = InformationNodeConverters.GetBuildWarnings(detail); | |
if (buildErrors.Count > 0) | |
{ | |
Console.WriteLine("Failure {0} / {1}", queued.TeamProject, queued.BuildDefinition.Name); | |
} | |
else if (buildWarnings.Count > 0) | |
{ | |
Console.WriteLine("Warning {0} / {1}", queued.TeamProject, queued.BuildDefinition.Name); | |
} | |
else | |
{ | |
Console.WriteLine("Success {0} / {1}", queued.TeamProject, queued.BuildDefinition.Name); | |
} | |
// clean up | |
watchList.Remove(queued.Id); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment