Problem
I needed to automate a full deployment of all my services, databases and the client that consumed them frequently.
Context
I am using an on premise installation of Team Foundation Server (TFS) 2012 and Visual Studio 2015. I needed to leverage multiple builds since msbuild arguments (the ones that you use to automatically publish/deploy) apply to every project/solution being built. This can potentially lead to multiple different builds.
Solution
Fortunately, this can be accomplished using PowerShell. TFS provides a good useful well-documented interface.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# TFS.psm1 | |
# | |
Function QueueBuilds { | |
param($serverName, $teamProject, [string[]]$buildDefinitionNames) | |
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client") | |
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client") | |
$serverName=$serverName | |
$teamProject=$teamProject | |
$tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($serverName) | |
$buildserver = $tfs.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer]) | |
foreach($buildDefinitionName in $buildDefinitionNames) { | |
Write-Output "Queuing build $buildDefinitionName" | |
$buildDefinition = $buildserver.GetBuildDefinition($teamProject, $buildDefinitionName) | |
$buildRequest = $buildDefinition.CreateBuildRequest(); | |
Write-Output $buildRequest | |
$result = $buildserver.QueueBuild($BuildReq) | |
} | |
} | |
Export-ModuleMember -Function 'QueueBuilds' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Queue Dev Builds | |
# | |
Import-Module $PSScriptRoot\TFS\TFS.psm1 | |
$buildNames = | |
"Service1 – Continuous", | |
"Service1.DB – Continuous", | |
"Service2 – Continuous", | |
"Service2.DB – Continuous", | |
"Service3 – Continuous", | |
"Client – Continuous" | |
QueueBuilds -serverName "http://tfs:8080/tfs/TeamProjectCollection" -teamProject "TeamProject" -buildDefinitionNames $buildNames |
Below are some helpful references that I found on my way to the above solution.
Update
I recently started working with a different TFS instance and version only to discover the script produced the below error. I have updated it accordingly.