我正在尝试将参数传递给MSBuild 2.0。经过研究后,似乎我需要使用变量来完成这项工作,但我无法弄清楚如何将其合并到下面的队列请求中。我试过参数,但似乎没有用。这是我试图告诉MSBuild @“/ p:OctoPackPackageVersion =”+ releaseNumber。这与使用IBuildRequest.ProcessParameters的XAML构建一起工作。
CreateMap<Source, Dest>();
CreateMap<Source, Child>();
答案 0 :(得分:2)
vNext构建系统与遗留XAML构建系统不同,在构建队列时,不能直接传递变量来构建构建定义中的任务。您使用的代码在构建队列之前更新了构建定义,这意味着如果变量发生更改,构建定义可能会不断更改。
解决方法是在构建定义中添加一个变量,例如“var1”,然后使用此变量作为MSBuild任务的参数: 这样,您可以在对构建进行排队时将值传递给“var1”变量,而无需更新构建定义。
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)
答案 1 :(得分:0)
首先,TFS2015上的新版本称为vNext,而不是MSbuild 2.0。
您要找的是Build variables。变量为您提供了一种方便的方法,可以将关键的数据部分放入构建过程的各个部分。对于选中允许在队列时间框中的变量,您可以启用允许您的团队在手动排队构建时修改该值。
有些教程可能对使用变量有帮助:
答案 2 :(得分:0)
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();
}
答案 3 :(得分:0)
我找到了这种解决方案,对我来说效果很好。为了方便在构建定义中设置自定义参数,而无需在服务器上进行更新:
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('\\');
}
然后在构建参数中设置变量值:
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;