【问题标题】:Queue another build then wait till build is finished排队另一个构建然后等待构建完成
【发布时间】:2014-11-06 17:31:23
【问题描述】:

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

不幸的是,我在谷歌上找不到任何可以帮助我的东西。下面是我用来排队构建的代码。

任何关于如何解决此问题的建议或反馈都将不胜感激。

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

【问题讨论】:

    标签: c# msbuild queue tfsbuild wait


    【解决方案1】:

    我可以通过编写一个 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;
    
        }
    }
    

    【讨论】:

      【解决方案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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-30
        • 2017-02-05
        • 1970-01-01
        • 2011-05-06
        • 1970-01-01
        • 1970-01-01
        • 2023-03-09
        • 1970-01-01
        相关资源
        最近更新 更多