Code Scraps: Send TeamCity the date and time during builds on Windows

Having the date and time available to you during a TeamCity build is very useful – but this information isn’t always available by default to TeamCity.

Here is a Windows Batch Script that can be called as part of the build configuration to set system properties you can then call in your build scripting:

@echo off
for /F “usebackq tokens=1,2 delims==” %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if ‘.%%i.’==’.LocalDateTime.’ set ldt=%%j
set t_year=%ldt:~0,4%
set t_month=%ldt:~4,2%
set t_day=%ldt:~6,2%
set t_hour=%ldt:~8,2%
set t_min=%ldt:~10,2%
set t_seconds=%ldt:~12,6%
REM Splits Microseconds from Seconds
For /F “tokens=1* delims=.” %%a IN (“%t_seconds%”) DO (
set t_second=%%a
set t_ms=%%b
)
REM Note that ‘t_second’ is just the second component. ‘t_seconds’ contains second and microseconds
echo ##teamcity[setParameter name=’system.current_date’ value=’%t_year%-%t_month%-%t_day%’] echo ##teamcity[setParameter name=’system.current_time’ value=’%t_hour%:%t_min%:%t_second%’] echo ##teamcity[setParameter name=’system.current_time_ms’ value=’%t_hour%:%t_min%:%t_seconds%’] echo ##teamcity[setParameter name=’system.current_filesafe_date’ value=’%t_year%%t_month%%t_day%’] echo ##teamcity[setParameter name=’system.current_filesafe_time’ value=’%t_hour%%t_min%%t_second%’] echo ##teamcity[setParameter name=’system.current_filesafe_time_ms’ value=’%t_hour%%t_min%%t_seconds%’]

When the above code is executed as part of a build configuration, you can then use any of the named parameters in any TeamCity build configuration field that supports parameters. It’s a good idea to make the above script the very first build step that’s called as part of a build, so that the parameters are immediately available for all subsequent builds.

You can also use these values in the Artifacts configuration box, so you can have the time properties as part of a naming convention for your artifacts – so long as the above script is called before the artifacts are generated.

This is the script for Windows – you can use the same principles for any OS. You may also be interested in my other Code Scrap on using Maven and Ant to send parameters to TeamCity.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.