Team Foundation Build Recipes

Using TFSDeployer for Staging Builds

Modified: 2009/06/11 04:29 by clively - Uncategorized
Edit

Description

TFS Deployer is a powerful tool in a build administrators arsenal. It allows you to deploy an entire web application by simply changing the build quality on the completed build. We use it to move our applications to the staging server once our developers are happy with the development build.

Edit

Requisites

Before starting, you need to have TFSDeployer installed on your build server. It can be found at: TFS Deployer on Codeplex Be sure to follow all of the installation instructions.

Note that TFS Deployer depends on PowerShell being installed on your build server.

Also, I make reference to 2 build qualities: "Development" and "In Staging". We changed all of the out of the box TFS build qualities. Be sure to update those strings in your own deployment script.

This depends on an existing build definition. If necessary create one for one of your solutions.

Finally, you need a share on the server you are deploying to that the user account TFS Deployer is running under has full access to.

Edit

Source

TFS Deployer

Edit

Usage

There are two script files below: StagingDeployment.ps1 and DeploymentMappings.xml. TFS Deployer works by reading the DeploymentMappings file to know what build quality changes to listen to. When a match is found, it will execute the supplied script and email anyone listed in the NotificationAddress list.

1. To start, create a folder under TeamBuildTypes// called Deployment. This will be at the same level as the TFSBuild.proj file.

2. Add the StagingDeployment.ps1 and DeploymentMappings.xml file to the folder.

3. Update the DeploymentMappings.xml file. Set the OriginalQuality and NewQuality attributes to match your build quality workflow. For example, if you want to deploy when the quality WAS Development and is now changed to In Staging add "Development" as the OriginalQuality and "In Staging" as the NewQuality. You can use an asterisk "*" for the OriginalQuality to match all.

4. Update the StagingDeployment.ps1 file. The primary things to be concerned with are the parameters to GetListOfBuilds and publish-site.

The first parameter for GetListOfBuilds is the TFS Project name (not to be confused with your web application project name). The second parameter is the Build name. NOTE: You will have to change the where {$_.BuildQuality -eq "In Staging"} clause to match the build quality name you are using.

For publish-site, the parameters are the source path, destination path, whether to rename the config file, and whether to delete the destination PRIOR to publishing.

5. Once all of this is done, check the changes into source control and test it.

Edit

Script

File: StagingDeployment.ps1
 
function get-tfs (
	[string] $serverName = $(Throw 'serverName is required')){
	# load the required dll
	[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
	$propertiesToAdd = (
		('VCS', 'Microsoft.TeamFoundation.VersionControl.Client', 'Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer'),
		('WIT', 'Microsoft.TeamFoundation.WorkItemTracking.Client', 'Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore'),
		('BS', 'Microsoft.TeamFoundation.Build.Common', 'Microsoft.TeamFoundation.Build.Proxy.BuildStore'),
		('CSS', 'Microsoft.TeamFoundation', 'Microsoft.TeamFoundation.Server.ICommonStructureService'),
		('GSS', 'Microsoft.TeamFoundation', 'Microsoft.TeamFoundation.Server.IGroupSecurityService')
	)
	# fetch the TFS instance, but add some useful properties to make life easier
	# Make sure to "promote" it to a psobject now to make later modification easier
	[psobject] $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($serverName)
	foreach ($entry in $propertiesToAdd) {
		$scriptBlock = '
			[System.Reflection.Assembly]::LoadWithPartialName("{0}") > $null
			$this.GetService([{1}])
		' -f $entry[1],$entry[2]
		
		$tfs | add-member scriptproperty $entry[0] $ExecutionContext.InvokeCommand.NewScriptBlock($scriptBlock)
	}
	return $tfs
}

function publish-site( [string] $sourcepath, [string] $destinationpath, [bool] $renameconfig, [bool] $deleteexisting) {
	$droplocation = $TfsDeployerBuildData.DropLocation
	
	$websourcepath = $droplocation + $sourcepath
	$webdestinationpath = $destinationpath
	
	new-item -force -path $webdestinationpath -itemtype "directory"
	if ($deleteexisting) {
		get-childitem $webdestinationpath | remove-item -force -recurse
	}
	get-childitem $websourcepath | copy-item -force -recurse -destination $webdestinationpath

	if ($renameconfig) {
		$configFile = $webdestinationpath + "web.production.config"
		remove-item $configFile -force
		$configFile = $webdestinationpath + "web.development.config"
		remove-item $configFile -force

		$configFile = $webdestinationpath + "web.staging.config"
		$configFileDest = $webdestinationpath + "web.config"
		move-item $configFile $configFileDest -force
	}
}

publish-site "\Release\_PublishedWebsites\MyWebApplication\" "\\vmwebstg\WebRoot\MyWebApplication\" 1 1

# now update the build qualities
$tfs = get-tfs http://csfoundation:8080

# first parameter is the TFS project name
# second parameter is the build name
$builds = $tfs.BS.GetListOfBuilds("Marketing", "My Build Name") | where {$_.BuildQuality -eq "In Staging"} | where {$_.BuildNumber -ne $TfsDeployerBuildData.BuildNumber}

foreach ($build in $builds) { 
	# Sometimes the BuildUri is null.  Not sure why.
	if ($build.BuildUri -ne $null) {
		$tfs.BS.UpdateBuildQuality($build.BuildUri, "Rejected") 
	}
}


File: DeploymentMappings.xml
<DeploymentMappings xmlns="http://www.readify.net/TFSDeployer/DeploymentMappings20061026">
  <Mapping xmlns=""
           Computer="csfoundationbld"
           OriginalQuality="*"
           NewQuality="In Staging"
           Script="StagingDeployment.ps1"
           RunnerType="PowerShell"
           NotificationAddress="someone@mycompany.com;someoneelse@mycompany.com" />
</DeploymentMappings>

Edit

Notes

You can change the build qualities at the Team Project level by right clicking on the Builds folder and selecting "Manage Build Qualities..."

© 2008 William Bartholomew blog.bartholomew.id.au

Powered by screwturn wiki