1

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

- 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 中的模板不起作用,即使它说值已被正确替换。

最好的

4

1 回答 1

0

如何为具有相同键的文件分配不同的值。

您可以使用名为Magic Chunks的扩展程序

这是一个例子:

transformations中,找到要分配给 using{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"
  }
}

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

使用 Replace Token 任务的逻辑与使用 File Transform 任务的逻辑不同。在 Replace Token 任务中,您需要将要替换的字符串放在特定的标记中(在tokenPrefix和中定义tokenSuffix)。另外,在 中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"
}
于 2020-12-23T03:26:54.090 回答