JSON Configuration Variables Feature

Last updated

The JSON configuration variables feature updates your JSON configuration files with the values from matching Octopus variables. In many ways this feature works similarly to the Substitute Variables in Files feature, but uses a special matching syntax so you can update configuration nested in JSON object and array literals.

ASP.NET Core 1.0 (previously known as ASP.NET 5) has been released! Microsoft's ASP.NET documentation site is the best resource on the ASP.NET Core web application platform. Their Configuration page provides detailed information on the configuration options available.

Our ASP.NET Core web application deployment guide provides further information on how this feature fits into an Octopus project's deployment process.

Configuring the JSON configuration variables feature

Step 1: Enable the feature

To configure a package step to replace matching app settings in your JSON configuration files ensure the JSON configuration variables feature is enabled.

Feature Configuration

Step 2: Configure which files to update

Next, specify the relative paths within the package to your JSON configuration files.

Relative path example
Your relative path may look something like:

approot\packages\ASPNET.Core.Sample\1.0.0\root\appSettings.json

Note that you can even use the Variable Substitution Syntax patterns in this file selection input box itself to do things like reference environment specific files, or conditionally include them based on scoped variables.

JSON variable subsitute

Any matching variables in the JSON file will have their values replaced with the variables you have configured in Octopus.

How your JSON configuration settings are updated by Octopus

When this feature is enabled, Octopus will find the target files and replace any matching configuration settings with the value of matching Octopus variables.

Simple variables

Given this example of a target config file:

Simple target file

{
   "weatherApiUrl": "dev.weather.com",
   "weatherApiKey": "DEV1234567",
   "tempImageFolder": "C:\temp\img"
}

If you have the Octopus Variables weatherApiUrl and weatherApiKey set to the values test.weather.com and TEST7654321 the target config file is updated to become, noticing the tempImageFolder setting remains untouched:

Simple JSON

{
   "weatherApiUrl": "test.weather.com",
   "weatherApiKey": "TEST7654321",
   "tempImageFolder": "C:\temp\img"
}

Hierarchical variables

It is common (and encouraged) to use hierarchical variables in JSON configuration files.  This is supported in Octopus Deploy variables by using a nested path syntax delimited by colon characters.

For example, to update the value of weatherApi.url and weatherApi.key in the target config file you would configure the Octopus Variables weatherApi:url and weatherApi:key.

Hierarchical JSON

{
   "weatherApi": {
      "url": "dev.weather.com",
      "key": "DEV1234567"
   }
}

You can also replace an entire object. For the example above you could set Octopus Variable weatherApi to a value of {"weatherApi":{"url":"test.weather.com","key":"TEST7654321"}}

Array variables

Octopus Deploy can also replace a value in a JSON array by using the zero-based index of the array in the variable name.

For example, the variable foo:bar:1 with a value qux will update the value of the second element in the array to be qux:

Hierarchical JSON

{
   "foo": {
      "bar": [
		"baz",
		"qux"
	  ]
   }
}

You can also replace an entire array. For the example above you could set Octopus Variable foo:bar to a value of ["baz","qux"]