Azure DevOps Pipeline中的配置、JSON文件中的变量替换。

问题描述 投票:-1回答:1

我对Azure DevOps有点陌生。我知道有一种方法,我们可以做XML转换和JSON变量替换。我们可以在库中定义键,值和json变量,这将在发布管道更新。例如,我定义了一个库值(hello = world),Release pipeline任务会在config, 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" />

enter image description here

azure-devops azure-pipelines-release-pipeline
1个回答
0
投票

是的,你可以使用token替换任务。请检查这个 延长

我使用了你的配置(我只是把 target folder 找到一个我保存文件的地方)与这个文件。

steps: 
- task: replacetokens@3
  displayName: 'Replace tokens in *.config *.json'
  inputs:
    targetFiles: |
     stackoverflow/23-token-replace/*.config
     stackoverflow/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>

一切都很顺利 这里你有一个 构建日志.

replacing tokens in: /home/vsts/work/1/s/stackoverflow/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.

一切看起来都很好。你确定你有一个正确的目标文件吗?**/*.config 意味着该扩展将寻找 any-folder-name/any-file-name.config 位于您的根文件夹中。


0
投票

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

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

1、你从构建管道发布的工件被压缩了。如果configjson文件驻留在压缩后的工件中,则无法被replacetoken任务找到。

所以你需要检查在发布管道中下载的工件是否是一个压缩文件。如果是压缩文件,你需要在文件中添加 提取文件任务 来解压它,然后再执行 replacetoken 任务。

您可以通过点击下面的 3d点 在下面的截图中突出显示。enter image description here

2、如果configjson文件不在默认工作目录下。你需要在 根目录 字段为configjson文件所在的文件夹的路径。

希望以上内容能帮助到大家

© www.soinside.com 2019 - 2024. All rights reserved.