配置中的变量替换,Azure DevOps 管道中的 JSON 个文件

Variable substitution in config, JSON files in Azure DevOps Pipeline

我对 Azure DevOps 有点陌生。我知道我们可以进行 XML 转换和 JSON 变量替换。我们可以在库中定义键、值和 json 变量,这将在发布管道中更新。有没有什么方法可以像替换任何文件中的变量值一样(配置,json)。例如,我定义了一个库值 (hello = world),Release pipeline 任务将在配置 json 文件中查找并替换 $hello 并将其替换为 "world"。 我正在尝试使用如下替换标记。

是的,我也在看同样的东西。看起来它没有替换值。您是否看到任何配置问题如下

步骤: - task: qetza.replacetokens.replacetokens-task.replacetokens@3 displayName: 'Replace tokens in **/*.config **/*.json' inputs: targetFiles: | **/*.config **/*.json verbosity: detailed tokenPrefix: '{' tokenSuffix: '}'

日志如下

2020-05-11T18:44:01.6149125Z ##[section]Starting: Replace tokens in **/*.config **/*.json 2020-05-11T18:44:01.6363261Z ============================================================================== 2020-05-11T18:44:01.6363986Z Task : Replace Tokens 2020-05-11T18:44:01.6364452Z Description : Replace tokens in files 2020-05-11T18:44:01.6364873Z Version : 3.6.0 2020-05-11T18:44:01.6365252Z Author : Guillaume Rouchon 2020-05-11T18:44:01.6365919Z Help : v3.6.0 - [More Information](https://github.com/qetza/vsts-replacetokens-task#readme) 2020-05-11T18:44:01.6366694Z ============================================================================== 2020-05-11T18:44:02.2020864Z pattern: \{\s*((?:(?!\{)(?!\s*\}).)*)\s*\} 2020-05-11T18:44:02.2247835Z replaced 0 tokens out of 0 in 0 file(s) in 0.04 seconds. 2020-05-11T18:44:03.1202699Z ##[section]Finishing: Replace tokens in **/*.config **/*.json 库值

myhello = Hello

appsettngs.config 中的值

<section name="{myhello}" type="Exceptionless.ExceptionlessSection, Exceptionless" />

是的。您可以使用令牌替换任务。请检查此 extension

我在这个文件中使用了你的配置(我只是更改 target folder 以找到我保存文件的地方)。

steps: 
- task: replacetokens@3
  displayName: 'Replace tokens in *.config *.json'
  inputs:
    targetFiles: |
     Whosebug/23-token-replace/*.config
     Whosebug/23-token-replace/*.json
    verbosity: detailed
    tokenPrefix: '{'
    tokenSuffix: '}'

配置文件:

<configuration>
  <configSections>
    <section name="sampleSection"
             type="System.Configuration.SingleTagSectionHandler" />
    <section name="{myhello}" type="Exceptionless.ExceptionlessSection, Exceptionless" />
  </configSections>
  <sampleSection setting1="Value1"
                 setting2="value two"
                 setting3="third value" />
</configuration>

一切顺利。这里有一个 log for a build.

replacing tokens in: /home/vsts/work/1/s/Whosebug/23-token-replace/appsettings.config
  using encoding: ascii
  myhello: Hello
  1 tokens replaced out of 1
replaced 1 tokens out of 1 in 1 file(s) in 0.033 seconds.

一切看起来都很好。你确定你有正确的 targetFiles 吗? **/*.config 表示扩展程序将查找位于根文件夹中的 any-folder-name/any-file-name.config

错误 replaced 0 tokens out of 0 in 0 file(s) in 0.04 seconds 表示在默认工作目录中找不到配置或 json 文件。

以下是在默认工作目录中找不到 config/json 文件的可能原因。

1,您从构建管道发布的工件已压缩。如果 config/json 文件驻留在压缩的工件中,则 replacetoken 任务无法找到它们。

因此您需要检查在发布管道中下载的工件是否为 zip 文件。如果它是压缩的,则需要在 replacetoken 任务之前添加 Extract files task 以将其解压缩。

您可以通过单击下面屏幕截图中突出显示的 3 点 轻松检查工件。

2、如果config/json文件不在默认工作目录下。您需要在 根目录 字段中指定 config/json 文件所在文件夹的路径。

希望以上内容对您有所帮助!