Search code examples
c#msbuildqueuetfsbuildwait

Queue another build then wait till build is finished


I am trying to write a MSBuild script that will queue another build and then will wait till that build is finished and then process further steps in the script after the queued build has finished. I would like the 1st build to continue in a running state until the queued build within this build has finished. Is it possible to do something like this? Would the script have to keep checking the build and it's status or would the queued build need to send back when it is finished to the 1st build?

I have not been able to find anything that could help me on google unfortunately. Below is the code I am using to queue the build.

Any suggestions or feedback would be appreciated on how to solve this issue.

<MSBuild.ExtensionPack.Tfs2010.TeamBuild    TaskAction="Queue"
                                            TeamFoundationServerUrl="$(TeamFoundationServerUrl)"
                                            TeamProject="Team_Proj"
                                            BuildDefinitionName="Build_Def"
                                            ContinueOnError="False" />

Solution

  • I was able to achieve this by writing a MSBuild custom task - see code below of task

    Referencing below link on the creation and use of this task

    Reference link:- http://blogs.msdn.com/b/msbuild/archive/2006/01/21/515834.aspx

    public class QueueBuild : ITask
    {
        public IBuildEngine BuildEngine
        { get; set; }
        public ITaskHost HostObject
        { get; set; }
    
        [Required]
        public string tfsServer
        { get; set; }
    
        [Required]
        public string teamProject
        { get; set; }
    
        [Required]
        public string buildDefinition
        { get; set; }
    
        public bool Execute()
        {
            // set up support for logging
            TaskLoggingHelper loggingHelper = new TaskLoggingHelper(this);
    
            // Log Variables
            loggingHelper.LogMessageFromText("Custom Task QueueBuild Starting", MessageImportance.High);
            loggingHelper.LogMessageFromText("tfsServer = " + tfsServer , MessageImportance.High);
            loggingHelper.LogMessageFromText("teamProject = " + teamProject , MessageImportance.High);
            loggingHelper.LogMessageFromText("buildDefinition = " + buildDefinition , MessageImportance.High);
    
            // Get the team foundation server
            TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(tfsServer);
    
            // Get the IBuildServer 
            IBuildServer buildServer = (IBuildServer)tfs.GetService(typeof(IBuildServer));
    
            // Get the build definition for which a build is to be queued
            IBuildDefinition buildDef = buildServer.GetBuildDefinition(teamProject, buildDefinition);
    
            // Create variable for queuedBuild and queue the build 
            var queuedBuild = buildServer.QueueBuild(buildDef);
    
            loggingHelper.LogMessageFromText("Waiting for newly queued build from Team Project : " + teamProject + " : and Build Definition : " + buildDefinition + " : to complete", MessageImportance.High);
    
            loggingHelper.LogMessageFromText("Pinging queuedBuild : " + queuedBuild + " : every 5 seconds to confirm when build is complete", MessageImportance.High);
    
            // Wait for the completion of newly queued build - Will ping build every 5 seconds to confirm completion for a max of 5 hours
            queuedBuild.WaitForBuildCompletion(TimeSpan.FromSeconds(5), TimeSpan.FromHours(5));
    
            loggingHelper.LogMessageFromText("Queued Build : " + queuedBuild.Build.BuildNumber + " has now completed", MessageImportance.High);
    
            loggingHelper.LogMessageFromText("Returning to original build", MessageImportance.High);
    
            return true;
    
        }
    }