Removing slashes from branch names in TeamCity
Asked Answered
I

1

10

I am trying to pass the branch names from TeamCity to OctopusDeploy so that we can easily track which branch a deployment came from.

To do this I want to append the branch name onto the version number (or the nuget package built with octopack) so that I can display this in the OctopusDeploy UI.

This works fine, except that we are using git-flow so some of our branches contain slashes which causes octopack to fail (as file names cannot contain slashes):

+:refs/heads/(feature/*)
+:refs/heads/(release/*)
+:refs/heads/(hotfix/*)

Is there any way to replace the slashes with something else in TeamCity without changing the way we name our branches?

Inflammation answered 29/10, 2015 at 16:0 Comment(0)
E
4

Using a build script you can interact with the build process and specify a custom build number where you can replace the slashes. For more details you can check the TeamCity docs.

Here you can find an c# example on how to alter the build number.

By example, in order to mangle the build number you can add CommonAssemblyInfo.cs with a content like (extracted from the above link):

$ww = ([Math]::Floor([DateTime]::Now.DayOfYear/7)+1)

Write-Host "##teamcity[buildNumber '%major.minor%.$ww.%build.counter%']"
$fileLocation = Join-Path -Path "%teamcity.build.checkoutDir%" -ChildPath "\SourceDir\AssemblyInfo.cs" 

$oldValue = "AssemblyFileVersion\(""(\d+)\.\d+\.\d+\.\d+""\)"
$newValue = [string]::Concat("AssemblyFileVersion(""%major.minor%.", $ww, ".%build.counter%", """)")

(get-content $fileLocation) | foreach-object {$_ -replace $oldValue, $newValue} | set-content $fileLocation
Elasticize answered 5/11, 2015 at 13:12 Comment(1)
Thanks, that helped. If you add some of the example code (I ended up using powershell, but anything is fine) to your answer so it doesn't depend on external resources then I'll award the bounty to you.Inflammation

© 2022 - 2024 — McMap. All rights reserved.