TeamCity, TFVC and Octopus Branching

I’ve recently implemented a TeamCity build on one of my old projects in TFVC and was surprised about the branching support in TeamCity is focused around Git and Mercurial for branching. So as per usual i coded my way out of a hole, and here’s how i did it.

This is just using a basic example of dev and main branch, but i normally use feature branching for this project too, and version/release branches in some of my other TFVC projects as well.

In TFVC my branches are just basically sub-folders to the solution root, and i put my tfignore at the root as this is outside of the core code imo

BranchLayoutBasic

In the VCS root, i use the root of the solution, so it includes all branches when it syncs, as TeamCity “syncs” files with source and doesn’t re-download them all the time like TeamBuild, its not an issue when you have a lot of branches, unless you need to do a clean build.

In the build Parameters create a Parameters called Branch Name

BuildParametersBranchName

Then Create a PowerShell step as the first step on the Build Configuration. Below example code checks the files that were included in the build change and sets the value for the parameter. If you run a build without a check-in it’s not going to have any changes attached to it, so in this case i default it to the dev branch.


$content = [IO.File]::ReadAllText($args[0])
if($content.length -eq 0)
{
Write-Host "No information, defaulting to Dev"
$branch = "dev"
Write-Host "##teamcity[setParameter name='BranchName' value='$branch']"
}
else
{
$branch = $content.Split("/")[0]
Write-Host "##teamcity[setParameter name='BranchName' value='$branch']"
}

This code will only look at the first file in the check-in, because it assumes you wont be checking in to multiple branches at once.

PowerShellStepBranchName

Once you’ve done this, you’ve got the branch name form the file system and can use it in your other steps

In my case i use it in the “OctopusDeploy: Create Release” Step

OctoCreateReleaseBranch

The end result in octopus looks like the below

OctopusBranch

In a subsequent post I’ll go into how to setup steps in octopus to prevent the dev branch from getting release to live.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s