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.

Youngjae
  • 24,352
  • 18
  • 113
  • 198
Robert Wagner
  • 17,515
  • 9
  • 56
  • 72

8 Answers8

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.

Jack Ukleja
  • 13,061
  • 11
  • 72
  • 113
  • 11
    Nice tip, Here is the format with milliseconds and path friendly `[DateTime]::Now.ToString("yyyyMMdd_hhmmssff")` – iraSenthil Mar 04 '14 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 Apr 20 '14 at 07: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? – The Senator Jun 06 '14 at 08:45
  • I face the problem with undefined property. So, where this should be defined? In Build steps or in Build Agent? And how to define it ib Build Agent if needed? – Alex Blokha Sep 25 '14 at 12:09
  • 2
    @Alex Blokha Just give it a temp value like "test", and it'll overwrite it properly – Bert Haddad Apr 26 '17 at 17:51
  • 1
    setting `buildNumber` directly with Service Message feature: `echo "##teamcity[buildNumber 'myBuildNumberHere']"` – Ricky Dec 28 '17 at 02:33
  • Could you please explain, how did you get date using this syntax? `[DateTime]::Now` – Yuri Beliakov Aug 14 '23 at 16:42
  • 24-hour time: `[DateTime]::Now.ToString("yyyyMMdd_HHmmssff")` – arni Aug 19 '23 at 10:13
  • Note that you can also define the `env.BUILD_START_TIME` variable under Project -> Parameters. TeamCity will suggest this automatically. – arni Aug 19 '23 at 10:28
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.

N0dGrand87
  • 727
  • 1
  • 9
  • 16
  • 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 Sep 19 '18 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.

c24w
  • 7,421
  • 7
  • 39
  • 47
Bilal
  • 922
  • 6
  • 7
  • @Bilal Could you summarise how to customize the date/time format now the link is broken, i can't find that info anywhere – BikerP Oct 10 '14 at 08:59
1

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

Marek
  • 1,688
  • 1
  • 29
  • 47
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.

CodeFox
  • 3,321
  • 1
  • 29
  • 41
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
maninvan
  • 890
  • 9
  • 10
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"
Youngjae
  • 24,352
  • 18
  • 113
  • 198
0

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.

Pankwood
  • 1,799
  • 5
  • 24
  • 43
GLaw
  • 104
  • 4
  • Could you provide more information for this system parameter and how to enable it? Is this documented somewhere? – CodeFox Sep 23 '20 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 Oct 07 '20 at 19:05
  • Interesting, I cannot find this parameter in my completed builds. Perhaps it is the secondary effect of a particular runner type? – CodeFox Oct 09 '20 at 07:22
  • I don't think it matters what runner type there is, as long as there's a build step. I just created a completely new test build, with the only a command line build step doing an "ls". If you look at parameters in your completed build, there are two sets of system parameters. The first is user-defined parameters, while the second is actual agent parameters. What version of TeamCity are you using? – GLaw Oct 13 '20 at 15:52
  • 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](https://youtrack.jetbrains.com/issue/TW-4080) as well as to the *Groovy plugin* and the *Formatted Date Parameter plugin* (see also my note [below](https://stackoverflow.com/a/64026152/1811525)). – CodeFox Oct 15 '20 at 13:57
  • Hmm. I'm using 2019.2.2 and any build with build steps that I have looked at has start time properties. For example: system.buildStartDate 2020/10/15:00:21:52 system.buildStartTime 20201015002152 We have a TC update scheduled. When we implement I'll try again and report back my findings. – GLaw Oct 15 '20 at 20:13
  • Thank you in advance, I am still curious where this parameter comes from. :-) Btw, in the meantime I have switched to the *Formatted Date Parameter plugin* (as I required the feature of formatting this value). – CodeFox Oct 16 '20 at 07:27
  • 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 Mar 25 '22 at 19:50
  • 1
    Thank you for the clarification! Interesting that there was such a field in the past. ;-) – CodeFox Mar 28 '22 at 10:07
  • You're welcome! It's a shame they removed the field. It's quite useful. – GLaw Mar 30 '22 at 01:21