Schedule TFS releases

I have been asked if TFS 2013 Release Management allows you to schedule TFS releases. Yes, you can schedule TFS release. What I mean is TFS allows you to schedule the deployment time of the release during the acceptance step of the release path. It's that easy.

But what if you want to schedule release to happen on a regular basis, for example you would like to automatically deploy/release the latest code to the development environment every Monday/Wednesday/Friday nights. TFS 2013 Release Management does not really have that feature. Luckily, we have reach TFS API and PowerShell. So, here is a PowerShell script that triggers release of the last successful build with the build quality set to "Ready for Deployment" (obviously, you can use any other filter to get the build you want):

param

(

    [string] $tfsCollectionPath = "http://SERVERNAME:8080/tfs/COLLECTIONNAME",

    [string] $tfsProjectName = "PROJECTNAME",

    [string] $buildDefinitionName = "BUILDDEFINITIONNAME",

    [string] $releaseTemplate = "RELEASETEMPLATENAME"

)

 

# Clear Output Pane

clear

 

# Enforce coding rules

Set-StrictMode -version 2.0

 

# Loads Windows PowerShell snap-in if not already loaded

if ( (Get-PSSnapin -Name Microsoft.TeamFoundation.PowerShell -ErrorAction SilentlyContinue) -eq $null )

{

    Add-PSSnapin Microsoft.TeamFoundation.PowerShell

}

 

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Common")  

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")  

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Common")  

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client")  

 

[Microsoft.TeamFoundation.Client.TfsTeamProjectCollection] $tfs = get-tfsserver $tfsCollectionPath

 

$tfsCollection = New-Object -TypeName Microsoft.TeamFoundation.Client.TfsTeamProjectCollection -ArgumentList $tfsCollectionPath

 

$server = new-object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection(New-Object Uri($tfsCollectionPath))

$buildServer = $server.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer])

$buildDetail = $buildServer.QueryBuilds($tfsProjectName, $buildDefinitionName)

 

$foundBuilds = $buildDetail | where {($_.Quality -eq "Ready for Deployment") -and ($_.Status -eq "Succeeded")} #| sort $_.BuildNumber

 

if ($foundBuilds -eq $null) 

{

    Write-Host "No builds found"

}

else

{

#$foundBuilds = $build | select BuildNumber, SourceGetVersion, Quality, DropLocation

 

    $LastReadyForDeploymentBuildNumber = $foundBuilds[-1].BuildNumber

    $LastReadyForDeploymentBuildDropLocation = $foundBuilds[-1].DropLocation

    Write-Host "Triggering build " $LastReadyForDeploymentBuild

 

    &"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Release Management\bin\ReleaseManagementBuild.exe" release -rt $releaseTemplate -pl $LastReadyForDeploymentBuildDropLocation

}

 

As you can see from the script, script first connects to TFS, finds the proper build definition, then looks for last successful and "Ready for Deployment" build, then uses ReleaseManagementBuild.exe to trigger the release for that build. Obviously, the script could be improved, for example, to include the exception handling, but it should enough to get you started.

Now that you have the script, you can use Windows Task Scheduler to trigger the release outside of the Release Management client. As often as you need it, whenever you feel like it.