Binding Dynamic Build Definitions to GitHub Repos

As part of the project that I am working on, I need to make sure that I allow the user to specify what GitHub repository they want to bind to their Visual Studio Team Services build definitions. I am using the Microsoft.TeamFoundationServer.Client library for that, but no matter what I tried, the repository just did not show up.


For the process, I was using a typical BuildHttpClient instance. All seemed smooth, but I always ended up with a failed repository binding in the VSTS UI:

Repo Binding Fail

And while I got the red exclamation mark there, I got the correct repository binding in the build definition preview:

Repo Binding

So what’s the deal? You’d likely use a code snippet like this:

BuildDefinition definition = new BuildDefinition();
definition.Name = configCarrier.Id.ToString();
definition.Project = new Microsoft.TeamFoundation.Core.WebApi.TeamProjectReference()
    Name = customParameters["project"],
    Id = new Guid(customParameters["projectId"])
definition.Repository = new BuildRepository()
    Id = $"{configCarrier.Repo.Url}.git",
    Type = "GitHub"
definition.Repository.Properties.Add("connectedServiceId", serviceEndpointId);
definition.Repository.Properties.Add("apiUrl", $"{repoOwner}/{repoName}");
definition.Repository.Properties.Add("cloneUrl", $"{repoOwner}/{repoName}.git");

Number one – make sure to add the .git prefix to the repository ID when you are creating a new BuildRepository. But that still won’t be enough. Take a look at the second part of the above code snippet:

definition.Repository.Properties.Add("gitLfsSupport", "false");
definition.Repository.Name = $"{repoOwner}/{repoName}";
definition.Repository.Url = new Uri($"{repoOwner}/{repoName}.git");
definition.Repository.DefaultBranch = "master";
definition.Repository.Clean = "null";
definition.Repository.CheckoutSubmodules = true;

Notice that you need to specify values for gitLfsSupport and fetchDepth. Without those properties in place the binding will not happen, so be careful!

Once these tweaks were made, I could successfully bind my repository to the definition.

One thought on “Binding Dynamic Build Definitions to GitHub Repos”

Leave a Reply

Your email address will not be published. Required fields are marked *