{tocify} $title={Table of Contents}
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. We even 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, then 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, let's 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 Jsonfile 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 their 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 the 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 the 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 😅✌👍🙌
It is good. Because I get the best information from your blog. DevOps Training Course in Delhi
ردحذف
ردحذفI get the best information from your blog. It is really good. DevOps Training Institute in Gurgaon