Modify Build process parameter by TFS API in C#

2019-08-12 20:31发布

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

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"

but how can I do this with C#?

Regards

1条回答
劫难
2楼-- · 2019-08-12 21:11

If you have assigned value already it will not change.

Try below:

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();
}
查看更多
登录 后发表回答