通过C#中的TFS API修改生成过程参数

本文关键字:过程 参数 修改 API 中的 TFS 通过 | 更新日期: 2023-09-27 17:59:13

我试图通过使用TFS API的C#程序设置构建过程参数。这个BuildDefinition是一个BuildDeployTest工作流(稍微修改一下LabDefaultTemplate.11.xaml)。我可以通过以下代码更改所有实验室流程设置(实验室工作流参数):

System.Collections.Generic.IDictionary<string, object> myDictionary = Microsoft.TeamFoundation.Build.Workflow.WorkflowHelpers.DeserializeProcessParameters(buildDefinition.ProcessParameters);
foreach (var Parameter in myDictionary)
{
    if (Parameter.Key == "LabWorkflowParameters")
    {
        // copy the entry
        Microsoft.TeamFoundation.Lab.Workflow.Activities.LabWorkflowDetails myCopy = Parameter.Value as Microsoft.TeamFoundation.Lab.Workflow.Activities.LabWorkflowDetails;
        foreach(TestAgentListItem testAgent in listOfTestAgents)
        {
            if(testAgent.Checked == true)
            {
                myCopy.EnvironmentDetails.LabEnvironmentName = testAgent.TestAgentName;
                myCopy.EnvironmentDetails.LabEnvironmentUri = new Uri(testAgent.LabEnvironmentUri);
                break;
            }
        }
        myDictionary[Parameter.Key] = myCopy;
    }
    break;
}
request.ProcessParameters = Microsoft.TeamFoundation.Build.Workflow.WorkflowHelpers.SerializeProcessParameters(myDictionary);
// trigger a new Build
buildServer.QueueBuild(request);

我有另一个名为"TestDirectory"的构建过程参数,它显示在构建过程参数->3下。杂项->测试目录。我试图通过更改此参数myDictionary["TestDirectory"] = @"TestDir";但它不会改变。在PowerShell中,我只需键入就可以更改此参数

[Microsoft.TeamFoundation.Build.Client.IBuildDefinition] $BuildDef = $buildserver.GetBuildDefinition($project,$buildDefinition)
[Microsoft.TeamFoundation.Build.Client.IBuildRequest] $BuildReq = $BuildDef.CreateBuildRequest();
$processParameters = [Microsoft.TeamFoundation.Build.Workflow.WorkflowHelpers]::DeserializeProcessParameters($BuildReq.ProcessParameters)
$processParameters.TestDirectory = "TestDir"

但是我如何用C#来实现这一点呢?

问候

通过C#中的TFS API修改生成过程参数

如果您已经分配了值,它不会改变。

试试下面的:

string argumentName = "TestDirectory";
var process = Microsoft.TeamFoundation.Build.Workflow.WorkflowHelpers.DeserializeProcessParameters(BuildDefinition.ProcessParameters);
if (process.ContainsKey(argumentName))                             
{
    process.Remove(argumentName);
    process.Add(argumentName, attributeValue);
    BuildDefinition.ProcessParameters = WorkflowHelpers.SerializeProcessParameters(process);
    BuildDefinition.Save();
}