Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace variables of JSON file in Team Services?

Tags:

azure-devops

I'm stuck with a release variable substitution of an angular project. I have a settings.json file which I would like to replace some variables:

{ 
    test : "variable to replace"
}

I tried to find some custom task on the marketplace but all of the tasks seems to work only with xml files for the web.config.

like image 965
gog Avatar asked Jun 27 '17 22:06

gog


2 Answers

I use the "Replace tokens" from the Marketplace https://marketplace.visualstudio.com/items?itemName=qetza.replacetokens

You define the desired values as variables in the Release Definition and then you add the Replace Tokens task and configure a wildcard path for all target text files in your repository where you want to replace values (for example: **/*.json). The token that gets replaced has configurable prefix and postfix (default are '#{' and '}#'). So if you have a variable named constr you can put in your config.json

{
   "connectionstring": "#{constr}#"
}

and it will deploy the file like

{
   "connectionstring": "server=localhost,user id=admin,password=secret"
}
like image 81
Frederic Avatar answered Sep 22 '22 21:09

Frederic


The IIS Web App Deploy Task in VSTS Releases has JSON variable substitution under *File Transforms & Variable Substitution Options. Provide a list of json files and JSONPath expressions for the variables that need replacing

For example, to replace the value of ‘ConnectionString’ in the sample below, you need to define a variable as ‘Data.DefaultConnection.ConnectionString’ in the build/release definition (or release definition’s environment).

{
  "Data": {
    "DefaultConnection": {
      "ConnectionString": "Server=(localdb)\SQLEXPRESS;Database=MyDB;Trusted_Connection=True"
    }
  }
}
like image 34
Justin Moore Avatar answered Sep 24 '22 21:09

Justin Moore