Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TeamCity to read and update version number from version file

We are using TeamCity as a CI server and we've been thinking how to implement a method for versioning our releases that is safe (i.e. minimal opportunity for human error) but also that requires the least effort possible. So far the following seems the most logical:

  1. Create a version file (or use assemblyinfo.cs). The version information would be in the format .... This file will be checked into my VCS.
  2. Run a TeamCity build
  3. TeamCity reads the version file from step 1, picks out each individual numbers and sets corresponding TeamCity build parameters (so I can use them later for things such as creating nuget packages and labelling)
  4. TeamCity auto increments the final 'build count' value.
  5. TeamCity saves the new version information back to the version file (with the updated 'build count')
  6. TeamCity then runs through the rest of my build steps, building files, running unit tests, setting the correct assembly versions etc.
  7. TeamCity finally checks the file back into VCS

We believe the benefit of doing things this way is should we need to do a fix of an old release then the version number will be correct and TeamCity can simply work with the old version file, and incrementing the build count as usual. This does make the assumption that we are updating our major/minor/patch versions correctly. This will hopefully work nicely with the soon to be released feature of TeamCity 7.1 that lets you choose which branch to build via the custom build dialog.

From what I've already read so far these sort of operations should be possible in TeamCity but we're looking for the easiest root to get this fixed as we're only a two-man outfit and we can't afford to invest large amounts of time into becoming experts at Nant or powershell only to find it might not do what we want.

So I guess to sum up my questions are as follows:

  1. Is what I am proposing possible?
  2. If yes then what is the best tool to use given my limited experience of Nant, powershell etc (i.e. which is the quickest to learn for someone whose command line and script experience extends as far as simple DOS operations and a bit of VBScript)

Any help is greatly appreciated.

like image 968
King Roger Avatar asked Aug 07 '12 16:08

King Roger


2 Answers

For your first question: yes, it's possible:

  1. Add .git(.svn, .hg etc) directory to VCS rules
  2. Read version number in your build script in teamcity(command line runner) in your favorite scripting language and incerement it.
  3. Set build number via interaction protocol
  4. Update build number in version file in your favorite scripting language
  5. Perform your build steps
  6. Perform commit and push, you can because you already have .git(.svn, .hg etc) directory in build dir in last step

Sorry but i can't answer for your second question because i don't know any appropriate tools for windows. On linux i would use bash.

like image 188
Fedor Gogolev Avatar answered Nov 11 '22 19:11

Fedor Gogolev


Add a build step using the PowerShell runner type

Set the Script field to Source code

In the ScriptSource field enter the following:

# Get AssemblyInfo file's contents
$file = "GlobalAssemblyInfo.cs"
$contents = get-content $file -Raw

# Regex to get the version number from AssemblyInfo file contents
$regex = new-object System.Text.RegularExpressions.Regex ('(^\s*\[\s*assembly\s*:\s*((System\s*\.)?\s*Reflection\s*\.)?\s*AssemblyVersion(Attribute)?\s*\(\s*@?\")(?<version>.*?)(\"\s*\)\s*\])', [System.Text.RegularExpressions.RegexOptions]::MultiLine)

# Get the version number
$version = $regex.Match($contents).Groups["version"].Value

# Update TeamCity buildNumber parameter using stdout
echo "##teamcity[buildNumber '$($version)']"
like image 39
Chris Morgan Avatar answered Nov 11 '22 20:11

Chris Morgan