Thursday, December 3, 2015

Setting a Build Parameter in TeamCity from within a Build Step

I recently had a requirement to set a TeamCity Build/Configuration parameter from one of my build steps so that I can use this value in a subsequent build step.

Normally, TeamCity has numerous built-in operations and values that you can leverage throughout your build configurations, but what about the scenario where a built-in TeamCity value or plug-in cannot meet that need?  Well, fortunately, TeamCity has a solution for that in the form of Service Messages!

When I first read this section of the documentation, however, I was extremely confused as to how to leverage this in my own Build Configuration.

First of all, I needed to create/define the necessary build parameter in my Build Configuration as defined int the documentation.  In this instance, I needed to create a System parameter called BUILD_TIMESTAMP.  

If I fail to define the required build parameter before running my build, I will receive an error stating that my build agent is unable to run a compatible build configuration, therefore, it is very important that you define the build parameters that you need ahead of time.

Once I had that in place, fortunately, a quick test in TeamCity led me to the answer I needed.

My particular requirement was to set a Build Timestamp, therefore, in my PowerShell script, I did something like the following:
$currentDate = Get-Date -Format yyyyMMdd


Write-Host "##teamcity[setParameter name='system.BUILD_TIMESTAMP' value='$currentDate']"

Now, when I want to use that value in any subsequent Build Steps, I can simply use the %system.BUILD_TIMESTAMP% value as a parameter anywhere else in my Build Configuration.

 A quick output in a subsequent PowerShell script Build Step yielded the required result!

Write-Host %system.BUILD_TIMESTAMP%

No comments:

Post a Comment