我正在使用VSTS/TFS2015的.NET Client Libraries,以编程方式创建基于我在另一个团队项目中获取的模板的构建定义。
我可以通过使用以下命令来获取构建定义模板(2.0):
BuildDefinitionTemplate builddeftemplate = buildHttpClient.GetTemplateAsync(teamProject, templateId).Result;我可以通过使用以下命令来创建构建定义:
BuildDefinition builddef = new BuildDefinition();
builddef.Project = newTeamProject;但看起来没有一种方法可以将模板作为构建定义的属性传递进来,也不能从模板创建构建定义。
查看REST API的文档时,GET请求实际上看起来像是返回了大量JSON:
{
"id": "vsBuild",
"name": "Visual Studio",
"canDelete": false,
"category": "Build",
"iconTaskId": "71a9a2d3-a98a-4caa-96ab-affca411ecda",
"description": "Build and run tests using Visual Studio. This template requires that Visual Studio be installed on the build agent.",
"template": {
"build": [
{
"enabled": true,
"continueOnError": false,
"alwaysRun": false,
"task": {
"id": "71a9a2d3-a98a-4caa-96ab-affca411ecda",
"versionSpec": "*"
},
"inputs": {
"solution": "**\\*.sln",
"msbuildLocation": "",
"vsLocation": "",
"msbuildArgs": "",
"platform": "$(BuildPlatform)",
"configuration": "$(BuildConfiguration)",
"clean": "false"
}
},
...因此,我认为可以只获取返回模板的一部分作为JSON对象,并通过带有这些部分的构建定义的POST,但看起来这必须是REST API路由。
.NET客户端库可以做到这一点吗?或者,有没有一种更简单的方法,我可能错过了?
发布于 2016-08-25 16:57:35
没有一种方法可以将模板作为构建定义的属性传递。然而,还有另一种方法来实现它。您可以通过.net库在团队项目之间克隆/导入/导出生成定义。
var cred = new VssCredentials(new WindowsCredential(new NetworkCredential(username, password)));
var buildClient = new BuildHttpClient(new Uri(collectionURL, UriKind.Absolute), cred);
var buildDef = (await buildClient.GetDefinitionAsync(sourceProj, buildDefId)) as BuildDefinition;
buildDef.Project = null;
buildDef.Name += "_clone";
await buildClient.CreateDefinitionAsync(buildDef, targetProj);从上面的代码中,您可以对团队服务器进行身份验证,并通过提供项目名称和构建定义id从源项目检索构建定义对象。
然后,您需要删除对项目的引用。由于生成定义包含对项目的引用,因此不可能将其导入到不同的项目中。最后,在目标项目中创建一个新的构建定义,提供从以前项目中检索到的定义对象。
下一步是将构建定义导出到一个文件,这样我们以后就可以导入它。使用json序列化程序序列化构建定义并将其保存到文件中。
var buildDef = (await buildClient.GetDefinitionAsync(project, buildDefId)) as BuildDefinition;
buildDef.Project = null;
File.WriteAllText(filePath, JsonConvert.SerializeObject(buildDef));最后添加一个导入方法,更多细节请参考此link
if (!File.Exists(filePath))
throw new FileNotFoundException("File does not exist!", filePath);
Console.WriteLine($"Importing build definition from file '{filePath}' to '{project}' project.");
var buildDef = JsonConvert.DeserializeObject<BuildDefinition>(File.ReadAllText(filePath));
buildDef.Name = newBuildName;
await buildClient.CreateDefinitionAsync(buildDef, project);https://stackoverflow.com/questions/39134708
复制相似问题