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
Table of contents
No headings in the article.
File transform using replace token Azure Devops extension, below is the link for the extension which we are going to use in this article.
Another article with default File Transform task is available here.
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.
- 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 below are variables used.
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