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" />

Answers


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;

    }
}

The accepted answer is great. Here is an update, it is an inline task for MsBuild and updated for the new TFS2012 Api. It also returns the build result:

    <UsingTask TaskName="QueueBuild" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <ParameterGroup>
        <tfsServer ParameterType="System.String" Required="true" />
        <teamProject ParameterType="System.String" Required="true" />
        <buildDefinition ParameterType="System.String" Required="true" />
        <buildResult ParameterType="System.Boolean" Output="true" />
    </ParameterGroup>
    <Task>
        <Reference Include="mscorlib" />
        <Reference Include="Microsoft.TeamFoundation.Build.Client" />
        <Reference Include="Microsoft.TeamFoundation.Build.Common" />
        <Reference Include="Microsoft.TeamFoundation.Client" />
        <Reference Include="Microsoft.TeamFoundation" />

        <Using Namespace="System.Diagnostics" />
        <Using Namespace="Microsoft.Build.Framework" />
        <Using Namespace="Microsoft.TeamFoundation.Client" />           
        <Using Namespace="Microsoft.TeamFoundation.Build.Client" />         
        <Using Namespace="Microsoft.TeamFoundation.Build.Common" />         
        <Code Type="Fragment" Language="cs">
        <![CDATA[           
                    // 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
                    var tfs = new TfsTeamProjectCollection(new Uri(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);

                    this.buildResult = queuedBuild.Build.Status == BuildStatus.Succeeded;
                    loggingHelper.LogMessageFromText("Build result:" + buildResult, MessageImportance.High);

                    return true;
                ]]>
        </Code>
    </Task>
</UsingTask>

And how to call it:

    <QueueBuild tfsServer="http://tfsServer:8080/tfs/Collection" teamProject="TeamProject" buildDefinition="MyBuild">
        <Output TaskParameter="buildResult" PropertyName="buildResult" />
    </QueueBuild>

Need Your Help

How do I prevent a popup form from taking focus from the parent

c# winforms .net-2.0

I have a small windows.form that I use to present information when the mouse is over a regions on a windows.Form, however it takes the focus from the parent window when it is set to visible. Is the...