The new build system in TFS or VSTS supports saving build definitions as templates. The problem is, that this only works inside a single TFS project. If you want to share your definition with other projects you have to use the REST API.
First you have to extract your current build definition. Get a list of all build definitions and check the ids.
$url = "https://<server>/tfs/DefaultCollection/ProjectName/_apis/build/definitions?api-version=2.0" (Invoke-RestMethod -Uri $url -Method Get -UseDefaultCredentials -ContentType application/json).value
Then use this ID to get the full definition.
$url = "https://<server>/tfs/DefaultCollection/ProjectName/_apis/build/definitions/18" $json = Invoke-RestMethod -Uri $url -Method Get -UseDefaultCredentials -ContentType application/json $json
Now save the json to a file and remove all the clutter like _links, revision etc. I’ve marked all the sections that you can remove in the following picture.
Then search for your project name and replace it with __ProjectName__.
That’s it. You can now use the following PowerShell to create the build definition in any project. The script loads the json for the build from a file, replaces the project name with the parameter and then creates the build definition in the project.
$json = Get-Content .\Build.json $json = $json.Replace("__ProjectName__", $ProjectName) $url = "https://<server>/tfs/DefaultCollection/$ProjectName/_apis/build/definitions?api-version=2.0" Invoke-RestMethod -Uri $url -Method Post -UseDefaultCredentials -Body $json -ContentType application/json
See this post if you want to use the script with VSTS. In this case you have to create a personal Access token.
Update: There is now a ExportImportBuildDefinition Extension available in the market place from Utkarsh Shigihalli. You can still use the REST API if you want to template your builds or move them from TFS to VSTS or vice versa.