johnson
johnson

Reputation: 11

Teamcity - automatic weekly incrementation of build number

has anybody a hint how it is possible to increment a second part of the number below ("249") on weekly basis? TeamCity is able to increase after every build the number of the build counter, so far so good.

1.0.249.%build.counter%

Additionally I need to increase the 249 on weekly basis automatically. eg.: 249 > 250; 250 > 251; 101 > 102; 41 > 42;...

Thanks in advance.

Upvotes: 1

Views: 2915

Answers (2)

Jim Hume
Jim Hume

Reputation: 31

While searching for a way to customize the build number in TeamCity I happened to stumble upon this question. The provided answer by @john-hoerr worked perfectly (thanks for that).

I have taken John's answer and modified it for my needs. Here is a powershell script that will update the build number using the provided build number format from the general tab of the build configuration settings, a date format (of any nature) and the build counter from TeamCity:

# The build number format is YYDDD.BB 
# YY is the last two digits of the year
# DDD is the day number in the year (1-365)
# BB is the build number of the day for this build type (1-N)

# Get the build number from TeamCity. This will contain whatever is placed 
# into the build number portion of the build definition
$TeamCityBuildNumber = "%build.number%"

# Get the build counter from TeamCity and ensure it's in the format 00 (for sorting).  
# Assumption: there are less than 100 builds a day for this build definition.
$TeamCityBuildCounter = "%build.counter%".PadLeft(2, "0")

# The build number really just contains the date portion of the whole build number string.  I am tweaking it to also contain a branch name but have removed this from my example code for simplicity purposes.
$BuildNumber = ([string]::Concat(([DateTime]::Now.Date.Year - 2000),([DateTime]::Now.DayOfYear)))

# Write the build number to the host, effectively altering the build number 
# within the process space of the build.
Write-Host "##teamcity[buildNumber '$TeamCityBuildNumber.$BuildNumber.$TeamCityBuildCounter']"

Ideally the build counter resets to 0 at midnight so that the first build of the day receives a build counter value of 1.

Upvotes: 0

John Hoerr
John Hoerr

Reputation: 8025

You can accomplish this by programatically re-defining the build.number in a PowerShell build step.

  1. Add a PowerShell build step as the first step in your configuration.
  2. Set the following parameters in that build step:
    • Script: Source Code
    • Script execution mode: Execute .ps1 script with "File" argument
    • Script source:
Write-Host "##teamcity[buildNumber '1.0.$([Math]::Floor([DateTime]::Now.DayOfYear/7)).%build.counter%']"

All subsequent build steps should be able to use the new build.number value.

The $([Math]::Floor([DateTime]::Now.DayOfYear/7)) is the magic; it produces the current week of the year. You'll need to massage this a little bit to conform to your existing numbering.

A note about reuse: You'll want to parameterize the 1.0 major/minor value in order to reuse this step across multiple projects. I'd define a parameter, %major.minor%, and set its value to 1.0 (or whatever.) The PowerShell script would then be this:

Write-Host "##teamcity[buildNumber '%major.minor%.$([Math]::Floor([DateTime]::Now.DayOfYear/7)).%build.counter%']"

Edit: Switched to a simpler Write-Host implementation as mentioned by Jacob.

Upvotes: 7

Related Questions