Search code examples
tfsbuildbuild-definitioncustom-activity

changing and saving ProcessParameters programatically TFS Build Definitions


TFS 2012 VS 2012 I have large number of Build Definitions that are derived from single template. I also have a Master Build to queue all those builds and pass arguments if I need to do so.

That part was done using TFS Community Extension: QueueBuilds.

Problem: Is there a way to access Build Definitions, loop through them, (can get up to here by myself) and change their ProcessParameters and save them.


Solution

  • You can use the Microsoft.TeamFoundation.Build.Client and related assemblies to update the process parameters via PowerShell or C#. The one tricky part is the parameters are stored in XML so you have to deserialize, make your changes, then serialize again to set them.

    This likely won't run without some tweaking but here are some snippets from one of my scripts that will help:

        [void][Reflection.Assembly]::LoadWithPartialName('Microsoft.TeamFoundation.Build.Client')
        [void][Reflection.Assembly]::LoadWithPartialName('Microsoft.TeamFoundation.Build.Workflow')
    $projectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($TFSUri)
    $buildServer = $projectCollection.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer])    $buildServer = GetBuildServer -TFSUri $TFSUri
    $buildDefinition = $buildServer.GetBuildDefinition($TeamProjectName, $BuildName);
    ...
    
      $parameters = [Microsoft.TeamFoundation.Build.Workflow.WorkflowHelpers]::DeserializeProcessParameters($buildDefinition.ProcessParameters)
      $msBuildArguments = $parameters.Get_Item("MSBuildArguments")
      $msBuildArguments = "$msBuildArguments /p:ImportParametersFilesOverride=true"
      $parameters.Set_Item("MSBuildArguments", $msBuildArguments)
      $parameters.Add("GetVersion", "c$TFSChangeSetNumber")
      $buildRequest.ProcessParameters = [Microsoft.TeamFoundation.Build.Workflow.WorkflowHelpers]::SerializeProcessParameters($parameters)