Azure DevOps — Replace tokens/key in JSON using File Transform in YAML Pipeline.

  1. Basic knowledge of how to create a repository on Azure DevOps.
  2. Basic knowledge of how to create a CI pipeline on Azure DevOps.

Let’s see how we can use File Transform task in Azure DevOps CI Pipeline to replace contents in a JSON file.

File Transform Task in Azure yaml pipeline
  1. Below is the Git Repository that I have on Azure DevOps, and the current appsettings.json values are highlighted
Git Repository on Azure
Variable Group in Azure
Create new Variable group
a. Variable block (Line 12–17)
Here, we have added the Library Variable group into Pipeline, we have to specify the group name.
Then, we have added the variable with name “MailSettings.EMail” and value is “$(EMail)”. This will read the value of EMail variable from Library.Since, the EMail key was defined under MailSettings key in appsettings.json, we have to use dot notation here to point to EMail key.b. PowerShell Task (Line 20–25)
This is a PowerShell task just to print the value of the Library Variable EMail.
c. File Transform Task (Line 27-32) - It is the File Transform task.folderPath specifies the solution name where the json file is.
fileType specifies the type of file used for File Transform.
targetFiles – Here, we specify the wildcard *.json, so, the File Transform will look for json files in our solution to do the replacement.
d. DotNet Core Task (Line 37-43) -
This is a DotNetCore Build and Publish task, which will build and publish our solution into a zip file.
e. Publish Artifacts Task (Line 45-50) -
This task will publish the above created artifact i.e. zip file into Pipeline, which we can download on local system and extract to see the appsettings.json file
Give Pipeline Permission
Artifact generated by Pipeline
Updated value in appsettings.json

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store