Azure Pipelines - 文件转换为具有相同密钥的不同文件设置不同的值

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

我正在尝试使用任务 FileTransform 修改 grafana json 模板的值。它正在通过以下方式修改一些键的值:

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

并用要替换的键声明了变量:

  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)

如果在 jsonTargetFiles 中我只声明一个文件它完美地工作,但我想知道如何为具有相同键的文件分配不同的值。

我试过使用“replaceTokens”,并在 jsons 文件中使用不同的变量名:

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

但是使用替换标记,grafana 中的模板不起作用,即使它说值已被正确替换。

最佳

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

您可以使用名为 Magic Chunks

的扩展程序

这是一个例子:

transformations中,使用{Node A}/{Node B}/...找到要赋值的变量并指定变量的值

- 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"
      }

目标 JSON 文件:

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

输出JSON文件:

{
  "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.

使用替换令牌任务的逻辑与使用文件转换任务的逻辑不同。在 Replace Token 任务中,您需要将要替换的字符串放在特定的标记中(在 tokenPrefixtokenSuffix 中定义)。另外,在variables中,需要将需要替换的字符串放在左边,将要替换的字符串放在右边。这是一个例子:

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

目标 JSON 文件:

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

输出JSON文件:

{
  "B": "disabled"
}