Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup TFS 2013 build definition to build from Git tag?

I want to create a special build definition in TFS 2013 to build from tag. The source control used in that project is Git.

So, let's say I have a tag called v1.0. I want this build definition to pull the sources corresponding to that tag and run a build. Triggers don't matter for now - it could be even manual. How is that possible?

I can see you only have an option to choose branch on the Source Settings tab...

Consider the advanced scenario: a build is triggered when a new tag is created, and takes the sources from that newly created tag to run a build. Is that possible? If so, how?

I failed to find any information besides plain default scenarios explained on MSDN. Probably, because the configuration (TFS 2013 in Git mode) is quite new...

Thanks in advance.

like image 480
Yan Sklyarenko Avatar asked Dec 11 '22 10:12

Yan Sklyarenko


1 Answers

I have done some investigations and played with what TFS offers by default. To be honest, it pretty much covers the basic scenario I described.

The default build template for Git contains a build argument called Checkout override:

enter image description here

This field accepts either tag name, or simply ID of the revision you'd like to build from:

enter image description here

The good thing here is that this setting overrides (just like the name suggests :)) the default branch. I mean, if you created a tag from master branch, but specified another branch on the Source tab of the build definition, it doesn't matter - the Checkout override takes preference.

I'll try to investigate the advanced scenario (described in my question). I suppose there will be some amount of custom code... will post the update here.

UPDATE DEC 23, 2013 As expected, in order to be able to choose the tag to build from, some custom code is required. I ended up creating a custom editor and assigning it to the Checkout override field. As a result, there's no option to paste any revision id there, only choose a tag from the list - but that's fine for my case.

So, first you should create a custom editor for a field. Basically, create a class, inherit it from System.Drawing.Design.UITypeEditor class and override a couple of methods. This walkthrough helped a lot, as well as this book (Chapter 18 "Customizing the Build Process").

The useful code which gets the list of tags from a specific Git repo of a specific TFS team project is here:

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>();
}

The DLL with the custom editor must be made visible to VS (in my case, I just put the assembly to the Common7\IDE\PrivateAssemblies\ folder of my VS installation). Then, in the field metadata editor you should specify the custom editor for the desired field:

enter image description here

And now if we edit the build definition, or queue a new build, we can select the necessary tag from the dropdown:

enter image description here

Hope this saves you some time.

like image 66
Yan Sklyarenko Avatar answered Feb 22 '23 01:02

Yan Sklyarenko