Search code examples
powershellteamcityassemblyinfo

How can I generate a custom build number in TeamCity and then patch it to assembly info?


I have done some searching on this topic and am able to find individual solutions to generate custom build numbers as well as patch assembly info, however I am unable to accomplish both. When using a custom POWERSHELL script that I found in a search, I am able to set the build number to what I create with the script, however this build number does not patch. The only success I have in patching is when using set numbers plus a counter. But the number that the POWERSHELL script creates does not persist to an extent that the Assembly patcher can work with. Am I doing it wrong?


Solution

  • I finally solved it with a little bit of Chaitanya's provided logic... but modified:

    $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