在 Team Build 的构建后事件中包含分支名称 [英] Include branch name in post build event on Team Build

查看:28
本文介绍了在 Team Build 的构建后事件中包含分支名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 TFS 构建过程中执行以下步骤:

I would like to perform the following steps in the TFS build process:

  • 执行构建后事件,将一些文件从我编译的项目复制到另一个预定义目录,我希望该目录路径包含分支名称.
  • 我也希望能够在我的 xaml 工作流模板中引用分支名称.

推荐答案

第一个比较简单.当您使用新的 TFS 2013 构建服务器和流程模板时,您只需在构建定义配置中添加构建后 powershell 脚本,签入脚本并在构建过程中运行它.

The first one is rather simple. When you're using the new TFS 2013 build server and process template, you can simply add a post-build powershell script in the Build Definition Configuration, check in the script and run it during the build.

第二个取决于您使用的是 TFVC 还是 Git,在第一种情况下,使用 VersionControlServer 类查询 BranchObjects,然后检查哪个是您工作文件夹的根目录.但请注意,在 TFVC 中,可以在一个工作区中引用多个分支,因此此查询可能有多个答案,具体取决于您使用哪个文件来查找分支根.自定义 CodeActivity 可以解决问题,类似于 此检查自定义签入政策.

The second one is dependent on whether you're using TFVC or Git, in the first case, use the VersionControlServer class to query the BranchObjects, then check which one is the root for your working folder. Be aware though, that in TFVC multiple branches can be referenced in one workspace, so there may be multiple answers to this query, depending on which file you use the find the branchroot. A custom CodeActivity would do the trick, similar to this check in a custom checkin policy.

代码将类似于:

 IBuildDetail buildDetail = context.GetExtension<IBuildDetail>();

 var workspace = buildDetail.BuildDefinition.Workspace;
 var versionControlServer = buildDetail.BuildServer.TeamProjectCollection.GetService<VersionControlServer>();

 var branches = versionControlServer.QueryRootBranchObjects(RecursionType.Full);
 var referencedBranches = listOfFilePaths.GroupBy(
                file =>
                branches.SingleOrDefault(
                     branch => file.ServerItem.StartsWith(branch.Properties.RootItem.Item)
                )
            ).Where(group => group.Key != null);

要获取 yo 工作区中所有项目的列表,您可以使用 Workspace.GetItems.

To get a list of all items in yo workspace, you can use Workspace.GetItems.

如果您使用 Git,您也有一些选择.最简单的是调用命令行:

In case you're using Git, you have a few options as well. The simplest is to invoke the command line:

 git symbolic-ref --short HEAD   

或深入LibGit2Sharp 并使用它来查找基于当前工作文件夹的分支名称自定义活动.

or dive into LibGit2Sharp and use it to find the branch name based on the current working folder from a custom activity.

如果您想将其包含在 MsBuild 任务中,这也是可能的.这个答案完全概述了所需的步骤有点远,但一旦你知道该怎么做,这并不难.

If you want to include this in an MsBuild task, this may well be possible as well. It goes a bit far for this answer to completely outline the steps required, but it's not that hard once you know what to do.

创建一个调用上述相同代码片段的自定义 MsBuild 任务,虽然不是通过 BuildDetail.BuildDefinition.Workspace 访问工作区,但是 通过 WorkStation:

Create a custom MsBuild task that invokes the same snippet of code above, though instead of getting access to the workspace through BuildDetail.BuildDefinition.Workspace, but through the WorkStation class:

Workstation workstation = Workstation.Current;
WorkspaceInfo info = workstation.GetLocalWorkspaceInfo(path);
TfsTeamProjectCollection collection = new TfsTeamProjectCollection(info.ServerUri);
Workspace workspace = info.GetWorkspace(collection);
VersionControlServer versionControlServer = collection.GetService<VersionControlServer>();

创建任务后,您可以创建一个自定义的 .targets 文件,该文件通过覆盖某些变量或在构建完成时复制数据来挂钩到 MsBuild 进程.您可以挂钩多个 Target 并定义是否需要在它们之前或之后执行某些操作.

Once the task has been created, you can create a custom .targets file that hooks into the MsBuild process by overriding certain variables or copying data when the build is finished. You can hook into multiple Targets and define whether you need to do something before or after them.

您可以将这些 导入到您的每个项目中,或者您可以将其放在ImportAfterImportBefore 文件夹中您的 MsBuild 版本以使其全局加载.您可以在此处找到该文件夹​​:

You can either <import> these into each of your projects, or you can place it in the ImportAfter or ImportBefore folder of your MsBuild version to make it load globally. You can find the folder here:

C:\Program Files (x86)\MSBuild\{MsBuild Version}\Microsoft.Common.Targets\ImportAfter

这篇关于在 Team Build 的构建后事件中包含分支名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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