TFS API:使用标签触发自定义构建时丢弃的错误



我正在尝试使用TFS API触发构建。我需要根据标签触发构建。代码就像:

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时发生错误:项目$/tfsprojname/buildprocessTemplates/newbuildprocesstemplate.xaml在版本labc_2.0.389@$/tfrojn中未在源控件中找到

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

您的问题类似于这种情况,请检查其中的解决方案:

我通过将丢失的构建过程模板添加到 我想构建的标签。因此,我基本上将逻辑替换为 以下:

// 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);
      }
    }
  }
}

最新更新