Get build date in C#

Answered

We have a C#.Net solution which is passed to CI and then deployment in TeamCity. On a main app page we'd like to display the build date-time. What should we install for TeamCity and which code we should use in C# to get the TeamCity environment and grab the build date/time?

6 comments

Hi Takhir,

I would use the FormattedDateParameter plugin to set the build date in a TeamCity variable, next I would use the File Content Replacer TeamCity build feature to stamp this build timestamp into a const variable of a static C# class. Finally use this static C# class in your code to display the build date.

FormattedDateParameter plugin:
https://github.com/sadgit/TeamCity-plugins/tree/master/FormattedDateParameter

Best regards,
Jvs

0

Hi Jvc,

Thank you for your valuable help! An hour earlier I've added another plugin, which allowed to grab these things:

%env.BUILD_START_DATE% %env.BUILD_START_TIME%

and in C# code I then retrieve them by:

ConfigurationManager.AppSettings["teamCity:build_number"]

where build_number is that whole string. The only odd thing is that only DEV environment can retrieve that build_number; others (STAGE, QA) don't have them, just null. Do you have any thoughts of why it may happen? Apparently, all deployments are similar, and I can't find any difference in their settings.

0

Hi Takhir,

From the C# statement I think you are retrieving the date from a xml file. From the TeamCity variable names I think you're using the GroovyPlugin.

The question is how does the build date (which resides in the TeamCity variable) get stamped into the xml file?

 Best regards,

Jvs

0

Your question is excellent, Jvs! And navigated me to the answer: so, actually when I compared the build steps in between different environments, I figured out that the difference is in how they communicate with application web.config file. The one that was working was reading the web.config file, then adding required variables and save them. On the website start the web.config was ready to grab the build date from it. Other environments didn't have those variables written in the config file, therefore nothing worked. I am posting the web config update script in case if some one needs that:

 

$webConfig = "C:\application\publish\web.config"
$doc = (Get-Content $webConfig) -as [Xml]
$el = $doc.CreateElement("add")
$kat = $doc.CreateAttribute("key")
$kat.psbase.value = "git:revision_number"
$vat = $doc.CreateAttribute("value")
$vat.psbase.value = "%build.vcs.number%"
$el.SetAttributeNode($kat)
$el.SetAttributeNode($vat)
$doc.configuration.appSettings.Appendchild($el)

$el = $doc.CreateElement("add")
$kat = $doc.CreateAttribute("key")
$kat.psbase.value = "teamCity:build_number"
$vat = $doc.CreateAttribute("value")
$vat.psbase.value = "%build.number%"
$el.SetAttributeNode($kat)
$el.SetAttributeNode($vat)
$doc.configuration.appSettings.Appendchild($el)

$el = $doc.CreateElement("add")
$kat = $doc.CreateAttribute("key")
$kat.psbase.value = "teamCity:build_config_name"
$vat = $doc.CreateAttribute("value")
$vat.psbase.value = "%system.teamcity.buildConfName%"
$el.SetAttributeNode($kat)
$el.SetAttributeNode($vat)
$doc.configuration.appSettings.Appendchild($el)

$doc.Save($webConfig)

0

Thank you, Jvs, you helped me a lot!!

0

You're welcome. I'm glad I could help you.

0

Please sign in to leave a comment.