在 C# 中通过 TFS API 修改构建过程参数 [英] Modify Build process parameter by TFS API in C#

查看:19
本文介绍了在 C# 中通过 TFS API 修改构建过程参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I tried to set a Build Process Parameter by a C# program which uses the TFS API. This BuildDefinition is a BuildDeployTest workflow (slightly modifie LabDefaultTemplate.11.xaml). I am able to change all the Lab Process Settings (Lab Workflow Parameters) by this code:

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. Misc -> TestDirectory 下.我试图改变这个参数myDictionary["TestDirectory"] = @"TestDir";但它不会改变.在 PowerShell 中,我只需键入

I have another Build process parameter called "TestDirectory" which is shown under Build process parameters -> 3. Misc -> TestDirectory. I tried to change this parameter by myDictionary["TestDirectory"] = @"TestDir"; but it doesn't change. In PowerShell I'm able to change this parameter by just typing

[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# 做到这一点?

but how can I do this with C#?

问候

推荐答案

如果你已经赋值了,它不会改变.

If you have assigned value already it will not change.

试试下面的:

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

这篇关于在 C# 中通过 TFS API 修改构建过程参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆