How to get / measure time in a build step?
Hard to believe this isn't easier - I just want to measure the elapsed time of a build, since it doesn't seem to be a built-in variable. I see several posts online that say to do this to record the start date, and then use that later to measure the elapsed time.
“##teamcity[setParameter name='env.BUILD_START_TIME' value='$([DateTime]::Now)']”
But that code generates an error for me:
##teamcity[setParameter name='env.BUILD_START_TIME' value='$([DateTime]
15:47:13 Error while parsing TeamCity service message: Value should end with "'". Valid service message has a form of "##teamcity[messageName name1='escaped_value' name2='escaped_value']" where escaped_value uses substitutions: '->|', [->|[, ]->|], |->||, newline->|n
15:47:13 ::Now)']
So how do you do this?
Thanks for any suggestions…
Please sign in to leave a comment.
Hi Sabibo,
You have the full build time and each build step's time in the build log in the TeamCity UI. You can enable the relative time option for a better presentation of the time spent:
Isn't it what you are looking for? If it doesn't suit your requirements, could you provide more details on what you want to achieve?
Best regards,
Anton
I need to send a Slack message at the end of the job to QA and our team - how do I access that value from a build step?
One option would be to get the build statistics via API; it has build duration and each build step duration: https://www.jetbrains.com/help/teamcity/rest/get-build-statistics.html#Get+Build+Statistic+Values.
Regarding the method you mentioned in the first message, it doesn't seem correct to me, as you are recording the execution of the first build step as a build start, while the build starts before the first step as soon as it is promoted from the queue to the build agent and changes collection from VCS starts.
Best regards,
Anton
One more option that you may consider.
1. Use email notifications to send notifications from TeamCity, as these notifications are customizable and can include much more detailed information: https://www.jetbrains.com/help/teamcity/customizing-notification-templates.html.
2. Slack allows setting up an email address for the channel. This way, all emails sent to this address will appear as messages on the channel: https://slack.com/intl/en-gb/help/articles/206819278-Send-emails-to-Slack.
So, when sending email notifications to the channel's email address, you will get much more detailed, customizable notifications.
Best regards,
Anton
Thanks for the replies - I will do the Rest API if that's the only way - but that sure is a long way to go to just get a single number that TeamCity clearly has the value for. Kind of ridiculous that it is not available. Regarding the method I mentioned in the first method, it may not be perfect but it would be close enough, if it worked. The problem is that the method generates an error.
FWIW I got my original attempt to work. It was just a matter of escaping the brackets around DateTime: so |[ and |].
But I wanted to also print out the value, so changed it to this:
current_time = datetime.datetime.now()
current_time_str = str(current_time)
print(f"Current time: {current_time_str}")
print(f"##teamcity[setParameter name='env.BUILD_START_TIME' value='{current_time}']")