【问题标题】:Variable substitution in config, JSON files in Azure DevOps PipelineAzure DevOps Pipeline 中的配置、JSON 文件中的变量替换
【发布时间】:2022-04-22 05:30:29
【问题描述】:

我对 Azure DevOps 有点陌生。我知道我们可以进行 XML 转换和 JSON 变量替换。我们可以在库中定义键、值和 json 变量,并在发布管道中更新。有什么方法可以像替换任何文件(config,json)中的变量值一样。例如,我定义了一个库值(hello = world),发布管道任务将在 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

appsetngs.config 中的值

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

【问题讨论】:

    标签: azure-devops azure-pipelines azure-pipelines-release-pipeline


    【解决方案1】:

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

    我使用了你的配置(我刚刚更改了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>
    

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

    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

    【讨论】:

    • 我根据您的建议更新了我的问题
    • 替换“{myHello}”的值“Hello”从何而来?
    • 这来自名为 myHello 的变量。可以直接在管道中声明,也可以在引用的变量组中声明。
    【解决方案2】:

    错误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解压。

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

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

    希望以上有帮助!

    【讨论】:

    • 是的,没错,我应该给出正确的文件和目录路径。我可以给一个自定义路径吗?我在原始问题中附上了图片。
    • 谢谢,如果我给出绝对路径,它工作得很好,C:\inetpub\devtesting
    • 两个答案都是正确的,我只能点击一个
    猜你喜欢
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-16
    相关资源
    最近更新 更多