Thursday 19 October 2023

Transform json Files in Azure Pipelines

 In .NET aplication we use the appsettings.json to keep values such as connection string etc for local development mostly. In the context of Azure app servies or containeried envronments etc, we configure mostly app config service (with key vault to manage secrets) to keep our connection information, and any other config values. Howver, sometimes legecy deployments which are still done targeting on premise servers etc might need us to update actual appsetting.josn files or web config files etc. Let's look at how to transform josn files in an Azure pipeline.

The expected outcome

The expectation is transforming the appsettings json file value based on a variable value defined in a release pipeline. We can of cource do the same in YAML pipline as well.

The pipline should substitute the values in json as shown below.


 How to do

Assume a josn file is containing below details.

{
  "Logging": {
    "LogLevel": {
      "Default""Information",
      "Microsoft.AspNetCore""Warning"
    }
  },
  "ConnectionStrings": {
    "UseDB""true",
    "ConnectionStringName""DefaultConnection",
    "DefaultConnection""Data Source=MYSVR\\SQL2019ENT;Initial Catalog=MYDB;Trusted_Connection=True;"
  },
  "mysettings": {
    "id""1111111",
    "name""TEST - My AB",
    "userName""admin@111111",
    "integrationName""Chaminda",
    "clientId""CHsecretid",
    "clientSecret""ChSecret"
  },
  "AllowedHosts""*"
}

And we just want to replace the default connection string, using a variable value. In a variable group we can define a variable with below name.

ConnectionStrings.DefaultConnection

Then we can use a trasform file task as shown below.

- task: FileTransform@2
  displayName: 'File Transform: '
  inputs:
    folderPath: '$(System.DefaultWorkingDirectory)/my_api/v1_API/my_api'
    jsonTargetFiles: appsettings.json

For the folder path you can provide a web deployment package zip name as below.

- task: FileTransform@2
  displayName: 'File Transform: '
  inputs:
    folderPath: '$(System.DefaultWorkingDirectory)/my_api/v1_API/my_api/webapi.zip'
    jsonTargetFiles: appsettings.json

I

No comments:

Popular Posts