File Transform Variable Substitution using replace token task in Azure Devops | Replace Token extension for Azure Devops | Substitute variable in json or xml config file | Transform/substitute config file - variables Azure Devops



{tocify} $title={Table of Contents}

File transform using replace token Azure Devops extension, below is the link for the extension which we are going to use in this article.

https://marketplace.visualstudio.com/items?itemName=qetza.replacetokens

Another article with default File Transform task is available here.

Azure Devops YAML File Transformation Task for JSON File End to End | Json File transformation task | Update appsettings json file with File Transformation Task in Azure Devops | End to End JSON File Transformation Task (lkgforit.com)

Let's go into how we are going to use this replace token extension task in Azure Devops for variable substitution.

Below is the sample code to substitute variables from pipeline into the file where the start of end of tag/token is passed and it takes the middle content as name of pipeline variable to replace in the file with variable value in that place.

Here in this example, we are using "{#" and "#}" as tokenPrefix and tokenSuffix respectively.

In the file wherever a content starts with this prefix and ends with end token suffix then the text is replaced with the value of a variable with same text as name.

Sample Code:


    - task: replacetokens@3
      inputs:
        targetFiles: "$(Pipeline.Workspace)/codepath/jsonFileName.json"
        encoding: "auto"
        writeBOM: true
        verbosity: "detailed"
        actionOnMissing: "warn"
        keepToken: false
        tokenPrefix: "{#"
        tokenSuffix: "#}"
      displayName: Perform variable substitution in json file

if we pass the below json file as input then it will replace pipeline variable values of variable1 and variable2 inside the tag.

{
  "headtag": "ens",
  "modelName": {
    "textProp": {
      "language": [
        {
          "property1": "{#Variable1#}",
          "property2": "{#Variable2#}"
        }
      ]
    }

For example, consider the variables used below.

variables:
  variable1: "value1" 
  variable2: "value2" 

then the output json file will be like below:

{
  "headtag": "ens",
  "modelName": {
    "textProp": {
      "language": [
        {
          "property1": "value1",
          "property2": "value2"
        }
      ]
    }

Thanks. 👍👌😋

Refer videos in my channel for live explanations.





YouTube Channel

https://www.youtube.com/channel/UCnYzqHl_und412jN2RTjJVA

Post a Comment

Previous Post Next Post

postad

post ad 2