6

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.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
gog
  • 11,788
  • 23
  • 67
  • 129

3 Answers3

6

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"
}
Frederic
  • 633
  • 5
  • 12
5

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"
    }
  }
}
Justin Moore
  • 827
  • 10
  • 12
  • But there's one question left: Can I also substitute protected variables? Because they aren't passed to the release tasks as environment variables? – Mike1991 Oct 18 '18 at 19:20
  • Where do you provide the list of json files ? – Bill Roberts Feb 08 '19 at 20:38
  • @flaZer The IIS Web App Deploy Task has an expandable section - File Transforms & Variable Substitution Options. In there is JSON variable substitution textarea for a line separated list of JSON files – Justin Moore Feb 14 '19 at 11:49
2

You can add a variable in release variables Tab, and then use PowerShell task to update the content of your settings.json.

Assume the original content is

{ 
    test : "old
}

And you want to change it to

{ 
    test : "new"
}

So you can replace the variable in json file with below steps:

1. Add variable

Define a variable in release variable tab with the value you want to replace with (variable test with value new):

enter image description here

2. Add PowerShell task

Settings for powershell task:

Type: Inline Script.

Inline Script:

# System.DefaultWorkingDirectory is the path like C:\_work\r1\a, so you need specify where your appsettings.json is.
$path="$(System.DefaultWorkingDirectory)\buildName\drop\WebApplication1\src\WebApplication1\appsettings.json"
(Get-Content $path) -replace "old",$(test) | out-file $path
Community
  • 1
  • 1
Marina Liu
  • 36,876
  • 5
  • 61
  • 74
  • 3
    Doesn't this require you to know the previous value, as opposed to the property name? This is very fragile. – Jeremy Holovacs Feb 05 '19 at 16:26
  • what if "test" values are different based on environments? e.g. for Dev "test" value is "devNew", for Stage "test" value is "stageNew" and for Prod "test" value is "prodNew". In this scenario how your solution will work? will you need to add three tasks for every environment and keep disable other two during deployment? – Ashish-BeJovial Aug 19 '19 at 08:14