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

Azure Devops YAML File Transformation Task for JSON File End to End

Table of contents

No heading

No headings in the article.

In this article we will see how to do appsettings json file transformation using Azure Devops YAML pipelines. In this article we will use FileTransformationTask@1 in YAML pipeline - reference.

Video explanation

We need to update the appsettings.json file on any dotnet core app deployment so that the configurations need to be updated as per environment. Even we use the environment json files for angular or react or any NodeJS apps so that we can configure values in the configuration json file with environment specific values using the automated CD pipeline using YAML.

Here we can see how to do that using the File transformation task in YAML Azure Devops pipelines.

# File transform
# Replace tokens with variable values in XML or JSON configuration files
- task: FileTransform@1
  inputs:
    #folderPath: '$(System.DefaultWorkingDirectory)/**/*.zip' 
    #enableXmlTransform: # Optional
    #xmlTransformationRules: '-transform **\*.Release.config -xml **\*.config-transform **\*.$(Environment.Name).config -xml **\*.config' # Optional
    #fileType: # Optional. Options: xml, json
    #targetFiles: # Optional

Above is the syntax for this yaml task, in which folderpath is the path of the solution or application folder in the repo, than if you want to xml transform then we have to set true in enableXmlTransform field, but in this example we are going to cover on json file transformation so we will leave this field which is optional. We will also leave xmlTransformationRules field which is also optional and only for xml transformation. In fileType field give json as value. In targetFiles field provide the json file path directly or relatively, so if you have multiple appsettings files inside folder path you have given in first field than giving relative path will see for all the appsettings json file like this: '**/appsettings.json'.

  # Update appsettings.json via FileTransform task.
- task: FileTransform@1
  displayName: 'File transformation: appsettings.json'
  inputs:
    folderPath: '${{ parameters.sourceDirectory }}'
    targetFiles: '**/appsettings.json'
    fileType: json

Ok the main part comes here, here we have updated the filetransform task, now how to update the values inside the appsettings json file with the environment specific configurations, lets see that now.

This file transform task will update the fields inside the appsettings json file from the available variables based on name of variable matching with the field inside the appsettings json file. For example, take the below json file as our app settings json file:

{
    "customFunctions": [],
    "importedLibraries": [],
    "firstResource": "",
    "groupOne": {
        "firstFieldInOne": "dev",
        "name": "sampleApp"
    },
    "groupTwo": {
        "nameTwo": "dummyValueTwo",
        "idTwo": ""
    },
    "runtimeSettings": {
        "storage": "",
        "telemetry": {
            "instrumentationKey": ""
        }
    }
}

So, in this appsettings file if you want to replace the parent elements of the json file then you should add the variables inside the yaml pipeline or as a pipeline variable with the name of the element - for example, to update 'firstResource' field you need to create a variable with name firstResource.

If you want to update inner child elements than you have to create variables with their path separated values by dot (.), here i mean there path values as element for which value need to be updated in second level child means the parent elements should be mentioned with dot(.) separated names, for this example, to update firstFieldInOne field which is a child of element groupOne we have to create a variable with name :

groupOne.firstFieldInOne

So, for above two values update we must create following variables in the yaml or in the pipeline variables:

variables:
  firstResource: "firstResourceValue"
  groupOne.firstFieldInOne: "FirstField Value"

So, for updating all the values in the above sample appsettings json file or any json configuration file we must follow this rule and create following variables:

variables:
  firstResource: "firstResourceValue"
  groupOne.firstFieldInOne: "FirstField Value"
  groupOne.name: "FirstField name"
  groupTwo.nameTwo: "name two Value"
  groupTwo.idTwo: "id two Value"
  runtimeSettings.storage: "storage account Value"
  runtimeSettings.telemetry.instrumentationKey: "instrumentation key"

If you use the above file transformation task and create these variables in the pipeline then you will see the following as the output json file which you can place in the deployment environment.

{
    "customFunctions": [],
    "importedLibraries": [],
    "firstResource": "firstResourceValue",
    "groupOne": {
        "firstFieldInOne": "FirstField Value",
        "name": "FirstField name"
    },
    "groupTwo": {
        "nameTwo": "name two Value",
        "idTwo": "id two Value"
    },
    "runtimeSettings": {
        "storage": "storage account Value",
        "telemetry": {
            "instrumentationKey": "instrumentation key"
        }
    }
}

Happy Coding :)

Refer videos in my channel for live explanations.

YouTube Channel