2013-10-17 94 views
5

我想在TFS 2013中创建一个特殊的构建定义,以从标记构建。该项目中使用的源代码控制是Git。如何设置TFS 2013构建定义从Git标记构建?

所以,假设我有一个标签v1.0。我想要这个构建定义拉对应于该标记的源并运行构建。触发器现在无关紧要 - 甚至可能是手动的。这怎么可能?

我可以看到你只有一个选项,选择源设置选项卡上的分支...

考虑高级方案:创建一个新的标签时构建被触发,并从取新来源创建标签来运行构建。那可能吗?如果是这样,怎么样?

除了在MSDN上解释的纯默认场景外,我找不到任何信息。可能,因为配置(TFS 2013在Git模式下)是相当新的...

在此先感谢。

回答

12

我已经做了一些调查,并使用TFS默认提供的功能。说实话,它几乎涵盖了我描述的基本场景。

GIT中的默认生成模板包含构建参数调用Checkout override

enter image description here

此字段接受变量名,或者干脆修改的ID,你想从建:

enter image description here

这里的好处是,这个设置覆盖(就像名称建议:))默认分支。我的意思是,如果您从主分支创建了一个标签,但在构建定义的“源”选项卡上指定了另一个分支,则无关紧要 - Checkout override优先。

我会尝试调查高级方案(在我的问题中描述)。我想会有一些自定义代码......会在这里发布更新。

更新2013年12月23日正如预期的那样,为了能够选择要构建的标记,需要一些自定义代码。我最终创建了一个自定义编辑器并将其分配给Checkout override字段。因此,无法粘贴任何修订ID,只能从列表中选择一个标签 - 但这对我的情况来说很好。

所以,首先你应该为一个字段创建一个自定义编辑器。基本上,创建一个类,从System.Drawing.Design.UITypeEditor类继承它并重写几个方法。 This walkthrough帮助了很多,以及this book(第18章“定制构建过程”)。

它获取的从特定TFS团队项目的特定的Git回购标签列表中的有用的代码是在这里:

private List<string> GetAvailableTags(IServiceProvider provider) 
{ 
    // obtain the current build definition object 
    var buildDefinition = (IBuildDefinition)provider.GetService(typeof(IBuildDefinition)); 
    // obtain the current source provider for the build definition (Git or TFVC) 
    var sourceProvider = buildDefinition.GetDefaultSourceProvider(); 

    // obtain the project collection 
    var teamProjectCollection = buildDefinition.BuildServer.TeamProjectCollection; 
    // obtain a service object to communicate with Git repo 
    var gitRepoService = teamProjectCollection.GetService<GitRepositoryService>(); 

    // this will get the partial URL of the Git repo (in a form <teamproject>/<repo>) 
    var repoUrl = sourceProvider.Fields[BuildSourceProviders.GitProperties.RepositoryName]; 

    string projectName; 
    string repoName; 

    // this is the way to parse the partial URL obtained above, into project name and repo name 
    if (BuildSourceProviders.GitProperties.ParseUniqueRepoName(repoUrl, out projectName, out repoName)) 
    { 
    // this will get all Git repos of the current team project 
    var source = gitRepoService.QueryRepositories(projectName); 
    // this will take the current Git repo we work with 
    var repo = source.First(x => x.Name.Equals(repoName, StringComparison.OrdinalIgnoreCase)); 
    // this will get all the tags in this Git repo 
    var tags = gitRepoService.QueryRefs(repo.Id, "tags"); 

    // and finally, the list of pure tag names is returned 
    return tags.Select(gitRef => gitRef.Name.Substring("refs/tags/".Length)).ToList(); 
    } 

    return new List<string>(); 
} 

与自定义编辑器中的DLL必须让用户看到它们VS(在我的情况,我只是将程序集放到我的VS安装的Common7\IDE\PrivateAssemblies\文件夹中)。然后,在该领域的元数据编辑器,你应该指定所需的字段的自定义编辑器:

enter image description here

现在,如果我们编辑构建定义,或排队一个新的版本,我们可以选择从必要的标签下拉菜单:

enter image description here

希望这可以节省一些时间。

+1

你会介意将此片段贡献给ALM Rangers Build Community Extensions吗? http://tfsbuildextensions.codeplex.com/ – jessehouwing

+0

这是一个好主意。你能指出它适合的确切位置吗? –

+0

我已经通知了维护这个的Ranger团队。如果您可以使用“上传补丁”功能上传带有源文件的zip文件,我想我可以设置流程以将它们包括在内...... http://tfsbuildextensions.codeplex.com/SourceControl/latest – jessehouwing

相关问题