1

I have a TeamCity build that passes some arguments to an .exe and runs it daily. One argument is for a date parameter, currently set to a static date. It now needs to be dynamic, passing in the current date.

I tried setting the value to %env.BUILD_START_DATE% but this makes all my agents incompatible because of an implicit requirement for that env variable. I also tried setting the date in the DOS command line script, skipping TC params altogether, but it still ends up with that implicit requirement.

4
  • Powershell has get-date. You could add a powershell script that sets the variables value that way.
    – user47589
    Commented Jun 19, 2018 at 20:51
  • Use some variation of the answers here: stackoverflow.com/questions/7019954/…
    – user47589
    Commented Jun 19, 2018 at 21:10
  • If you need current date in you exe then why do you have to pass it from TeamCity? Build start and current date will be just seconds away.
    – Peska
    Commented Jun 20, 2018 at 6:07
  • The exe needs to accept any date. For my implementation it needs the current date but other implementation need to pass it a static/specific date. Commented Jun 20, 2018 at 16:00

3 Answers 3

5

The top answer here: TeamCity Current Date variable in MMdd format indicated need for a TC plugin, the second answer, however, did not require a plugin and is mostly complete. How I made it work on a variation of that second answer:

1.) Add a powershell build step to run the following:

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

2.) Give env.BUILD_START_DATE a default value in the Environment Variables section. Without the default value TC thinks having this value is an implicit requirement of a build agent, rendering all of them incompatible.

1

If you are unfamiliar with PowerShell, here is a Bash approach.

You can set a parameter with script and retrieve the value from the next build step.

echo "##teamcity[setParameter name='env.BUILD_START_DATE' value='$(date +%%Y-%%m-%%dT%%H:%%M:%%S%%z)']"

The doubled %% is because TeamCity considers % as its own parameter in build script.

The parameter will have ISO8601 format timestamp. For other formats, please see various Bash date format at here; https://www.tutorialkart.com/bash-shell-scripting/bash-date-format-options-examples/

0

You can set the variable in the config, which should be overridden during runtime. I don't see env.BUILD_START_DATE in my TC parameters, but I do see system.buildStartTime. I set it to a dummy value (I use "[Filled Automatically]"), and everything works fine. Build gets run and system.buildStartTime gets overridden at build time.

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