Over a year ago I’ve presented a solution to copy build definitions between team projects in new Team Foundation Server 2015. That was a workaround for missing functionality in TFS, so there was no standard way to save the build definition as a template and reuse it outside the scope of current team project. Well, in TFS 2017 it hasn’t change yet…
There is still no way to reuse build definitions between team projects directly from the web portal of TFS. However thanks to its rich REST api this is quite easy to do it programatically, for instance with use of Powershell scripting facilities. The TFS REST api is well documented here.
I use the build rest api which allows to get, put and delete build definitions as well as build templates for all team projects. The powershell script looks like this
Assumming we have a team project “MyProject” and a build definition template in it called “dev” and we have a team project “SecondProject”, we can easily copy the build definition template using above function. We do it like this:
We will be asked for the credentials with a visual prompt. As you can see the script removes some of the unnecessary project specific data from the template, so it better fits to the new team projects. It also searches for all team project name strings and replaces them with the new one. After that we should already see the build definition template available in “SecondProject” when creating a new build definition