2016-11-24 19 views
0

我試圖使用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 = "[email protected]$/TFSProjName"; 
buildServer.QueueBuild(req); 

在我的生成定義,生成過程模板的服務器路徑是提供(這不是LabelName我正在上面提供部分)。 在運行時,它顯示了以下錯誤:

TF215097: An error occurred while initializing a build for build definition \TFSProjName\MyTestBuild: Item $/TFSProjName/BuildProcessTemplates/NewBuildProcessTemplate.xaml was not found in source control at version [email protected]$/TFSProjName.

當我使用Visual Studio觸發同樣的身材,它工作正常。我不知道如何明確指示系統檢查BuildProcessTemplate這不是我提供的標籤的一部分。

回答

1

您的問題類似於this case,請在它的解決方案:

I fixed the problem by adding the missing build process template to the label I want to build. So, I basically replace the logic with the following:

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

感謝@cece - MSFT。有效。 –

相關問題