Azure Pipelines - File Transform set different values to different files with the same key

Viewed 2414

I am trying to use the task FileTransform to modify the values of grafana json templates. And it's working for modifying the values of somes keys the following way:

- task: FileTransform@2
  displayName: "Transform Jsons"
  inputs:
    folderPath: 'metrics/dashboards/**/'
    xmlTransformationRules: ''
    jsonTargetFiles: '**/*.json'

And having declared the variables with the keys to substitute:

  templating.list.0.query: $(azureClusterName)
  templating.list.0.current.text: $(azureClusterName)
  templating.list.0.current.value: $(azureClusterName)
  templating.list.0.options.0.text: $(azureClusterName)
  templating.list.0.options.0.value: $(azureClusterName)

If in jsonTargetFiles I only declare one file it works perfectly, but I want to know how can I assign different values for files that have the same keys.

I've tried using "replaceTokens", and having different variable names inside of the jsons files:

- task: replacetokens@3
  displayName: 'Replace tokens'
  inputs:
    rootDirectory: 'metrics/dashboards'
    targetFiles: '**/*.json'
    encoding: 'auto'
    verbosity: 'detailed'
    actionOnMissing: 'fail'
    tokenPrefix: '#{'
    tokenSuffix: '}#'

But with replace tokens the template in grafana doesn't work even it says that the values have been replaced correctly.

Best

1 Answers

How can I assign different values for files that have the same keys.

You can use a extension called Magic Chunks

Here is an example:

In transformations, Find the variable you want to assign to using {Node A}/{Node B}/... and specify the value of the variable

- task: MagicChunks@2
  inputs:
    sourcePath: '{target json file path}'
    fileType: 'Auto'
    targetPathType: 'source'
    transformationType: 'json'
    transformations: |
      {
        "ConnectionStrings/DefaultConnection": "Data Source=10.0.0.5;Initial Catalog=Db1"
      }

Target JSON file:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=webapp"
  }
}

Output JSON file:

{
  "ConnectionStrings": {
    "DefaultConnection": "Data Source=10.0.0.5;Initial Catalog=Db1"
  }
}

But with replace tokens the template in grafana doesn't work even it says that the values have been replaced correctly.

The logic of using the Replace Token task is different from using the File Transform task. In the Replace Token task, you need to place the string you want to replace in specific tokens (defined in tokenPrefix and tokenSuffix). In addition, in variables, you need to place the strings need to be replaced on the left and the strings used to replace on the right. Here is an example:

variables:
  enabled: disabled

- task: replacetokens@3
  inputs:
    targetFiles: 'A.json'
    encoding: 'auto'
    writeBOM: true
    verbosity: 'detailed'
    actionOnMissing: 'fail'
    keepToken: false
    tokenPrefix: '#{'
    tokenSuffix: '}'
    useLegacyPattern: false
    enableTelemetry: true

Target JSON file:

{
  "B": "#{enabled}"
}

Output JSON file:

{
  "B": "disabled"
}
Related