TFS API:使用标签

时间:2016-11-24 13:54:48

标签: c# .net tfs msbuild tfs2010

我正在尝试使用TFS API触发构建。我需要触发基于Label的构建。代码就像:

WorkItemStore workItemStore = tfs.GetService<WorkItemStore>();
Project teamProject = workItemStore.Projects["TFSProjName"];
IBuildServer buildServer = tfs.GetService(typeof(IBuildServer)) as IBuildServer;
IBuildDefinition buildDef = buildServer.GetBuildDefinition(teamProject.Name, "MyTestBuild");
IBuildRequest req = buildDef.CreateBuildRequest();
req.GetOption = GetOption.Custom;
req.CustomGetVersion = "LABC_2.0.389@$/TFSProjName";
buildServer.QueueBuild(req);

在我的构建定义中,提供了构建过程模板的服务器路径(这不是我在上面提供的LabelName的一部分)。 运行时,它会显示以下错误:

  

TF215097:初始化构建定义\ TFSProjName \ MyTestBuild的构建时发生错误:在版本LABC_2.0.389 @ $ / TFSProjName的源代码管理中找不到Item $ / TFSProjName / BuildProcessTemplates / NewBuildProcessTemplate.xaml。

当我使用Visual Studio触发相同的构建时,它工作正常。我不确定如何明确指示系统检查BuildProcessTemplate哪个不是我提供的标签的一部分。

1 个答案:

答案 0 :(得分:1)

您的问题与this case类似,请查看其中的解决方案:

  

我通过添加缺少的构建过程模板来解决问题   我要建立的标签。所以,我基本上用了替换逻辑   以下内容:

// Check if a label was specified for the build; otherwise, use latest.
if (!labelName.IsEmptyOrNull())
{
  // Ensure the build process template is added to the specified label to prevent TF215097.
  AppendBuildProcessTemplateToLabel(tpc, buildDefinition, labelName);
  // Request the build of a specific label.
  buildRequest.GetOption = GetOption.Custom;
  buildRequest.CustomGetVersion = "L" + labelName;
}
I created the following method to append the build process template to the label prior to queueing a build.

/// <summary>
/// Appends the build process template to the given label.
/// </summary>
/// <param name="teamProjectCollection">The team project collection.</param>
/// <param name="buildDefinition">The build definition.</param>
/// <param name="labelName">Name of the label.</param>
private static void AppendBuildProcessTemplateToLabel(TfsConnection teamProjectCollection, IBuildDefinition buildDefinition, string labelName)
{
  // Get access to the version control server service.
  var versionControlServer = teamProjectCollection.GetService<VersionControlServer>();
  if (versionControlServer != null)
  {
    // Extract the label instance matching the given label name.
    var labels = versionControlServer.QueryLabels(labelName, null, null, true);
    if (labels != null && labels.Length > 0)
    {
      // There should only be one match and it should be the first one.
      var label = labels[0];
      if (label != null)
      {
        // Create an item spec element for the build process template.
        var itemSpec = new ItemSpec(buildDefinition.Process.ServerPath, RecursionType.None);
        // Create the corresponding labe item spec element that we want to append to the existing label.
        var labelItemSpec = new LabelItemSpec(itemSpec, VersionSpec.Latest, false);
        // Create the label indicating to replace the item spec contents. This logic will append
        // to the existing label if it exists.
        versionControlServer.CreateLabel(label, new[] {labelItemSpec}, LabelChildOption.Replace);
      }
    }
  }
}