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

﹥>﹥吖頭↗ 提交于 2021-01-27 05:47:01

问题


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

回答1:


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



回答2:


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;



回答3:


First, the new build on TFS2015 which is called vNext build not MSbuild 2.0.

Which you are looking for should be Build variables. Variables give you a convenient way to get key bits of data into various parts of your build process. For the variable with Allow at queue time box checked you could be enable allow your team to modify the value when they manually queue a build.

Some tutorials may be helpful for using variables:

  • TFS Build 2015 (vNext) – Scripts and Variables

  • Passing Visual Studio Team Services build properties to MSBuild




回答4:


Patrick, I was able to find a work around to my issue by updating the build definition. This is definitely not ideal but it works. As you can see below I am trying to add to the msbuild args already present. If you know a better way let me know. I really appreciate you taking the time to look at my question.

 public static async Task<string> QueueNewBuild(string project, BuildDefinitionReference targetBuild, string collectionURL, string ChangeSetNumber, string ReleaseNumber, bool CreateRelease)
    {
        var buildClient = new BuildHttpClient(new Uri(collectionURL), new VssCredentials(true));
        await Task.Delay(1000).ConfigureAwait(false);
        var buildDef = await buildClient.GetDefinitionAsync(targetBuild.Project.Id, targetBuild.Id);
        BuildDefinitionVariable OrigMSbuildvar = buildDef.Variables["MSBuildArgs"];
        buildDef.Variables["MSBuildArgs"].Value = OrigMSbuildvar.Value + " /p:OctoPackPackageVersion=" + ReleaseNumber.ToString();

        await Task.Delay(1000).ConfigureAwait(false);
        buildDef = await buildClient.UpdateDefinitionAsync(buildDef);

        await Task.Delay(1000).ConfigureAwait(false);
        Build build = new Build
        {
            Definition = new DefinitionReference
            {
                Id = targetBuild.Id
            },
            Project = targetBuild.Project,
            SourceVersion = ChangeSetNumber
        };

        await Task.Delay(1000).ConfigureAwait(false);
        var res = await buildClient.QueueBuildAsync(build);
        buildDef.Variables["MSBuildArgs"].Value = OrigMSbuildvar.Value;           
        await Task.Delay(1000).ConfigureAwait(false);
        buildDef = await buildClient.UpdateDefinitionAsync(buildDef);
        return res.Id.ToString();

    }


来源:https://stackoverflow.com/questions/44784909/tfs-build-2-0-c-howto-add-variables-to-the-build-pass-msbuild-args

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!