3

I am using team city 9.1.7 version on Windows 2012 server. As part of the build steps, I build a nodejs based application using command line. The output is bunch of Javascript and html files.

In the next step (after the build is over & output is generated), I want to perform following:

  1. Take the current build number from team city and insert it into index.html (available in output folder) file. I want to add a meta tag which can tell me the build version.
    1. In the same file (index.html), I want to perform string find and replace operations. I want to add time stamp to files.

Find this <script src="bundle.js"></script> and

replace with <script src="bundle.js?time=getTime()"></script>

will results in <script src="bundle.js?time=4324324324"></script>

SharpCoder
  • 18,279
  • 43
  • 153
  • 249

1 Answers1

12

Try the following

Add a PowerShell step and run the following as source code

$versionNumber = "%build.number%"
$filePath = "%teamcity.agent.work.dir%\path\file.txt"

(GC $filePath).Replace("<head>", "<head><meta http-equiv='X-Version-Number' content='$versionNumber'>").Replace("bundle.js", "bundle.js?time=getTime()") | Set-Content $filePath

This will read the file contents in and perform two replacements on them and then write back to the file.

![enter image description here

Not sure what your file path is or what you want the header called, but you should be able to change this to suit your requirements.

Hope this helps

REVISION

To catch any exceptions, try wrapping the code in a try catch block

try {
    (GC $filePath).Replace("<head>", "<head><meta http-equiv='X-Version-Number' content='$versionNumber'>").Replace("bundle.js", "bundle.js?time=getTime()") | Set-Content $filePath
}

catch [System.Exception] {
    Write-Output $_
    Exit 1
}

To break out of the cache you could use the version number as this will increment each build and thus be unique

try {
    (GC $filePath).Replace("<head>", "<head><meta http-equiv='X-Version-Number' content='$versionNumber'>").Replace("bundle.js", "bundle.js?v=$versionNumber") | Set-Content $filePath
}

catch [System.Exception] {
    Write-Output $_
    Exit 1
}
Matt
  • 3,684
  • 1
  • 17
  • 19
  • Thank you for the reply and adding the screenshot. I made the required changes but for some reason script ran without any error but it did not produced the desired result. Is there any alternate way to test the script. Also i dont want to hard code the file path like, `c:/file.txt`. Can we make it relative to team city working directory ? – SharpCoder Jul 27 '16 at 19:13
  • Try running this script locally from the ISE. You can use $filePath = "%teamcity.agent.work.dir%\path\file.txt" within the context of the build configuration to use a path relative to where the agent is working. – Matt Jul 27 '16 at 19:21
  • Thank you for quick reply. When i executed the script by copying and pasting in power shell, i got the desired result. Not sure why it did not worked via team city I am seeing an extra option `Script execution mode:` which you have not shown in your screenshot. It has two option `1. execute script as external .ps1 file 2. put script into power shell`. I have chosen option 1. – SharpCoder Jul 27 '16 at 19:32
  • Another thing i noticed was after conversion, i saw this `bundle.js?time=getTime()` which means getTime() was not evaluated. and was treated as a string – SharpCoder Jul 27 '16 at 19:36
  • I beg your pardon - I misunderstood - TBH, you could just inject the build number again as it's only being used to break out of the cache. I will update the answer – Matt Jul 27 '16 at 19:40
  • This is simple and awesome !! – SharpCoder Jul 27 '16 at 20:36
  • 1
    to get the working dir, I am now using `%teamcity.build.checkoutDir%` . This actually points to correct working dir – SharpCoder Jul 28 '16 at 15:57
  • @EvolveSoftwareLtd - This is perfectly working. But the strange part is if the file is not modified/updated then it wont run this command or the file wont get updated with the buildnumber. Any solution would be much appreciated. – Guru Prasad Jul 03 '19 at 13:02