jetbrains-ideteamcity-9.0teamcity-8.0

using team city to insert build number & perform string replacement operations


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>


Solution

  • 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
    }