排队另一个构建然后等待构建完成

时间:2014-11-06 17:31:23

标签: c# msbuild queue tfsbuild wait

我正在尝试编写一个MSBuild脚本,它将对另一个构建进行排队,然后等待该构建完成,然后在排队构建完成后再处理脚本中的其他步骤。我希望第一个版本继续处于运行状态,直到此版本中的排队构建完成。可以这样做吗?脚本是否必须继续检查构建及其状态,或者排队的构建是否需要在完成第一次构建时发回?

遗憾的是,我无法在Google上找到任何可以帮助我的内容。下面是我用来对构建进行排队的代码。

如何解决此问题,我们将不胜感激。

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

2 个答案:

答案 0 :(得分:2)

我能够通过编写MSBuild自定义任务来实现这一点 - 请参阅下面的任务代码

在创建和使用此任务时参考以下链接

参考链接: - 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;

    }
}

答案 1 :(得分:2)

接受的答案很棒。
这是一个更新,它是MsBuild的内联任务,并为新的TFS2012 Api进行了更新 它还返回构建结果:

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

如何称呼它:

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