Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TFS Build 2.0 C#, howto add variables to the build/Pass Msbuild args

I am trying to pass arguments to MSBuild 2.0. After research it appears that I need to do this using variables, but I cannot figure out how to incorporate this into my queue request below. I have tried parameters but that does not seem to work. Here is what I am trying to tell MSBuild @" /p:OctoPackPackageVersion=" + releaseNumber. This worked with the XAML build using IBuildRequest.ProcessParameters.

var buildClient = new BuildHttpClient(new Uri(collectionURL), new 
VssCredentials(true));
var res = await buildClient.QueueBuildAsync(new Build
            {
                Definition = new DefinitionReference
                {
                    Id = targetBuild.Id
                },
                Project = targetBuild.Project,
                SourceVersion = ChangeSetNumber,
                Parameters = buildArg

            });
            return res.Id.ToString();
like image 471
Kirit Chandran Avatar asked Jun 27 '17 15:06

Kirit Chandran


People also ask

What is TFS build?

A build definition is the mechanism that controls how and when builds occur for team projects in TFS. Each build definition specifies: The things you want to build, like Visual Studio solution files or custom Microsoft Build Engine (MSBuild) project files.

What is TFS build system in Azure?

Previously known as Team Foundation Server (TFS), Azure DevOps Server is a set of collaborative software development tools, hosted on-premises. Azure DevOps Server integrates with your existing IDE or editor, enabling your cross-functional team to work effectively on projects of all sizes.

What is the difference between MSBuild and Visual Studio build?

Visual Studio determines the build order and calls into MSBuild separately (as needed), all completely under Visual Studio's control. Another difference arises when MSBuild is invoked with a solution file, MSBuild parses the solution file, creates a standard XML input file, evaluates it, and executes it as a project.

Can I install multiple self hosted agents on the same machine?

Self-hosted agents Also, machine-level caches and configuration persist from run to run, which can boost speed. Although multiple agents can be installed per machine, we strongly suggest to only install one agent per machine.


2 Answers

vNext build system is different with legacy XAML build system, you cannot pass variable to build tasks in the build definition directly when queue the build. The code you used updated the build definition before queue the build which means that the build definition may keep changing if the variable changed.

The workaround for this would be add a variable in your build definition for example "var1" and then use this variable as the arguments for MSBuild Task: enter image description here With this, you will be able to pass the value to "var1" variable when queue the build without updating the build definition.

Build build = new Build();
build.Parameters = "{\"var1\":\"/p:OctoPackPackageVersion=version2\"}";

// OR using Newtonsoft.Json.JsonConvert
var dict = new Dictionary<string, string>{{"var1", "/p:OctoPackPackageVersion=version2"}};
build.Parameters = JsonConvert.SerializeObject(dict)
like image 156
Eddie Chen - MSFT Avatar answered Oct 17 '22 17:10

Eddie Chen - MSFT


I have found this solution and it works for me excellent. I set custom parameters for convenience in build definition without updating on server:

foreach (var variable in targetBuildDef.Variables.Where(p => p.Value.AllowOverride))
        {
            var customVar = variables.FirstOrDefault(p => p.Key == variable.Key);
            if (customVar == null)
                continue;
            variable.Value.Value = customVar.Value.TrimEnd('\\');
        }

And then set variables values in build parameters:

        using (TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri(tFSCollectionUri)))
        {
            using (BuildHttpClient buildServer = ttpc.GetClient<BuildHttpClient>())
            {
                var requestedBuild = new Build
                {
                    Definition = targetBuildDef,
                    Project = targetBuildDef.Project
                };
                var dic = targetBuildDef.Variables.Where(z => z.Value.AllowOverride).Select(x => new KeyValuePair<string, string>(x.Key, x.Value.Value));
                var paramString = $"{{{string.Join(",", dic.Select(p => $@"""{p.Key}"":""{p.Value}"""))}}}";
                var jsonParams = HttpUtility.JavaScriptStringEncode(paramString).Replace(@"\""", @"""");
                requestedBuild.Parameters = jsonParams;
                var queuedBuild = buildServer.QueueBuildAsync(requestedBuild).Result;
like image 2
Alexander Phoenix Avatar answered Oct 17 '22 18:10

Alexander Phoenix