27

In TeamCity is there an easy way to get a variable for the current date in the format MMdd (eg 0811 for 8-Aug)?

My google-fu did not turn up an existing plugins. I looked into writing a plugin, but not having a jdk installed, that looks time consuming.

8 Answers 8

47

This is quite easy to do with a PowerShell build step (no plugin required) using the following source code:

echo "##teamcity[setParameter name='env.BUILD_START_TIME' value='$([DateTime]::Now)']"

or (for UTC):

echo "##teamcity[setParameter name='env.BUILD_START_TIME' value='$([DateTime]::UtcNow)']"

This uses TeamCity's Service Message feature that allows you to interact with the build engine at runtime e.g. set build parameters.

You can then reference this build parameter from other places in TeamCity using the syntax %env.BUILD_START_TIME%

The advantage of this approach is you don't need to use a plugin. The disadvantage is you need to introduce a build step.

9
  • 11
    Nice tip, Here is the format with milliseconds and path friendly [DateTime]::Now.ToString("yyyyMMdd_hhmmssff")
    – iraSenthil
    Commented Mar 4, 2014 at 14:43
  • 13
    You must also define env.BUILD_START_TIME in the agent's buildAgent.properties file. Otherwise TeamCity won't match an agent as compatible.
    – seldary
    Commented Apr 20, 2014 at 7:55
  • @seldary, thanks for the tip here on the defining of the property in the build agent. How do I actually ensure that the powershell value is pushed to the build agent, if I define the env property this overwrites the build servers value? Commented Jun 6, 2014 at 8:45
  • 2
    @Alex Blokha Just give it a temp value like "test", and it'll overwrite it properly Commented Apr 26, 2017 at 17:51
  • 1
    setting buildNumber directly with Service Message feature: echo "##teamcity[buildNumber 'myBuildNumberHere']"
    – Ricky
    Commented Dec 28, 2017 at 2:33
11

For Unix based build agents I propose next custom script as one of build commands:

export current_build_date_format="+%%Y.%%m.%%d"
export current_build_date="$(date $current_build_date_format)"
echo "##teamcity[setParameter name='env.current_build_date' value='$current_build_date']"

You have to make double % sign to avoid interpretation for date executable command line argument FORMAT string (see %Y.%m.%d) as already existing TeamCity variable.

1
  • Not as of writing, obviously, but PowerShell core is now available for Linux. We haven't had time to experiment with Non-Windows build agents, so I'm unclear how well this would actually working trying to invoke a PS script on a Linux agent.
    – StevoInco
    Commented Sep 19, 2018 at 15:18
9

The Groovy Plugin for TeamCity provides build start date/time properties:

Provides build properties:

system.build.start.date / env.BUILD_START_DATE

system.build.start.time / env.BUILD_START_TIME

This blog post has installation / configuration instructions for the Groovy plugin, as well an example of customizing the date/time format.

1
  • @Bilal Could you summarise how to customize the date/time format now the link is broken, i can't find that info anywhere
    – BikerP
    Commented Oct 10, 2014 at 8:59
1

An old question, but for those looking for a solution now there is a system parameter available.

system.buildStartTime

You need to declare it in config (it's not available until runtime) in order to run. I set mine to value [Filled Automatically]

As you can guess, this time is set to the build start time, so that may not be ideal for some needs. But it's easy and reliable.

10
  • Could you provide more information for this system parameter and how to enable it? Is this documented somewhere?
    – CodeFox
    Commented Sep 23, 2020 at 10:34
  • Sorry I don't know of any documentation. If you look at the parameters in a completed build you'll see it as a system property. It is a parameter for every build, but does not exist until runtime.
    – GLaw
    Commented Oct 7, 2020 at 19:05
  • 1
    Thanks for getting back! Today I have double checked the actual agent parameters (with TeamCity 2020.1.4) without any luck. That's why I have asked the folks at JetBrains and they told me, that there is no such predefined parameter called system.buildStartTime at the moment but referred me to feature request TW-4080 as well as to the Groovy plugin and the Formatted Date Parameter plugin (see also my note below).
    – CodeFox
    Commented Oct 15, 2020 at 13:57
  • 1
    So it's been a long time, sorry - I forgot about this thread. On 2021.1 I verified that these fields do not exist. They do exist in 2019.2 system.buildStartDate 2021/03/25:08:48:46 system.buildStartTime 20210325084846
    – GLaw
    Commented Mar 25, 2022 at 19:50
  • 1
    Thank you for the clarification! Interesting that there was such a field in the past. ;-)
    – CodeFox
    Commented Mar 28, 2022 at 10:07
1

You can also try Date Build Number plug-in. It povides additional var in build number format rather than build property.

1

Similar to the Date Build Number plugin mentioned in this answer, there exists a derived plugin called Formatted Date Parameter. It provides a customizable parameter build.formatted.timestamp that can be used out of the box in fields or other parameters. No need for a separate build step.

0

To add a dated folder to my build in TeamCity I added the following to my custom script. What had me stuck was the double % sign in the date string. D'oh

TARGET_DIR=/Users/admin/build/daily
TARGET=$(date "+%%Y-%%m-%%d")

if [ ! -d ${TARGET_DIR} ]; then
  mkdir -vp ${TARGET_DIR}/
fi
mv -v build.dmg ${TARGET_DIR}/build_${TARGET}.dmg
0

If you only want to have one-line bash command in a build step, just use as below.

echo "##teamcity[setParameter name='build.timestamp' value='$(date +%%m%%d)']"

(double % symbol is for TeamCity own escape rule to use % character)

It will set a MMdd parameter value right after the execution during runtime so very useful to put at any build step. Then, you can retrieve a parameter value afterward.

Note that you should create build.timestamp parameter firstly to TeamCity project.

A step further, I made a simple bash script to have bash date format timestamp. This script will set timestamp to whatever bash supported datetime format and parameter name to TeamCity.

name=""  # TeamCity parameter name
format="%Y-%m-%dT%H:%M:%S%z"  # ISO8601 format by default
result=""  # a TeamCity parameter value to be set

for ARGUMENT in "$@"
do
    KEY=$(echo "$ARGUMENT" | cut -f1 -d=)
    VALUE=$(echo "$ARGUMENT" | cut -f2 -d=)

    case "$KEY" in
            name)              name=${VALUE} ;;
            format)     format=${VALUE} ;;
            *)
    esac
done

result=$(date "+$format")

echo "##teamcity[setParameter name='$name' value='$result']"

Below usage will set ISO8601 format timestamp to build.timestamp parameter.

./teamcity_datetime.sh name=build.timestamp

If you want to set only MMdd, the execution could be as below.

./teamcity_datetime.sh name=build.timestamp format="%%m%%d"

Not the answer you're looking for? Browse other questions tagged or ask your own question.