使用TFS 2015 .NET客户端库从模板创建生成定义 [英] Create Build Definition from Template with TFS 2015 .NET Client Libraries

查看:39
本文介绍了使用TFS 2015 .NET客户端库从模板创建生成定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 .NET客户端库以便VSTS/TFS 2015以我在另一个团队项目中获取的模板为基础,以编程方式创建构建定义.

I'm using the .NET Client Libraries for VSTS/TFS 2015 to programmatically create a build definition based off of a template that I've grabbed in another team project.

我可以使用以下方法获取构建定义模板(2.0):

I can get a build definition template (2.0) by using:

BuildDefinitionTemplate builddeftemplate = buildHttpClient.GetTemplateAsync(teamProject, templateId).Result;

我可以使用以下方法创建构建定义:

And I can create a build definition by using:

BuildDefinition builddef = new BuildDefinition();
builddef.Project = newTeamProject;

但是,似乎没有办法将模板作为构建定义的属性来传递,也没有从该模板创建构建定义的方法.

But there doesn't look like a way to pass in a template as a property of the build definition, nor create a build definition from the template.

查看REST API文档时,GET请求实际上看起来像返回了许多JSON:

When looking at the documentation for the REST API, the GET request actually looks like it returns a lot of 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路由.

So I think that it might be possible to only grab parts of the returned template as a JSON object and pass through a POST of the build definition with those parts, but it seems like that would have to solely be the REST API route.

.NET客户端库可能吗?还是有一种我可能错过的更简单的方法?

Is this possible with the .NET Client Libraries? Or is there an easier way that I might have missed?

推荐答案

没有办法将模板作为构建定义的属性来传递.但是,还有另一种方法可以实现它.您可以通过.net库在团队项目之间克隆/导入/导出构建定义.

There isn't a way to pass in a template as a property of the build definition. However, there's another way to achieve it. You can clone/import/export build definition between team projects through .net libraries.

    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从源项目中检索构建定义对象.

From above code you can authenticate to the team server and retreive the build definition object from the source project by the providing project name and the build definition id.

然后您需要删除对该项目的引用.由于构建定义包含对项目的引用,因此无法将其导入到其他项目中.最后,在目标项目中创建一个新的构建定义,提供从先前项目中获取的定义对象.

And then you need to remove the reference to the project. Since build definition contains a reference to the project it would not be possible to import it into a different project. Finally create a new build definition in target project providing the definition objecte retreived from previous project.

下一步是将构建定义导出到文件,以便稍后导入.通过使用json序列化程序序列化构建定义并将其保存到文件.

Next step is to export the build definition to a file so we can latter import it. By using a json serializer to serialize the build definition and save it to a file.

   var buildDef = (await buildClient.GetDefinitionAsync(project, buildDefId)) as BuildDefinition;
   buildDef.Project = null;
   File.WriteAllText(filePath, JsonConvert.SerializeObject(buildDef));

最后添加一个导入方法,更多详细信息请参考此

Finally add a import method, more details please refer this 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);

这篇关于使用TFS 2015 .NET客户端库从模板创建生成定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆