'Variable substitution in config, JSON files in Azure DevOps Pipeline
I am a bit new in Azure DevOps. I know there is way that we can do XML transformation and JSON variable replacement. We can define key, value and json variable in library and that would update in release pipeline. Is there any way like just replacing a variable value in any file (config, json). for example I define a library value (hello = world), and Release pipeline task would find and replace $hello in config, json file and replace it with "world". I am trying to use replace tokens as below.
Yes I was looking at same. Looks like its not replacing the values. Do you see any configuration issue as below
steps:
- task: qetza.replacetokens.replacetokens-task.replacetokens@3
displayName: 'Replace tokens in **/*.config **/*.json'
inputs:
targetFiles: |
**/*.config
**/*.json
verbosity: detailed
tokenPrefix: '{'
tokenSuffix: '}'
log as below
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
Library value
myhello = Hello
Value in appsettngs.config
<section name="{myhello}" type="Exceptionless.ExceptionlessSection, Exceptionless" />
Solution 1:[1]
Yes. You can use token replace task. Please check this extension
I used your configuration (I just changed target folder
to find a place wher I keep files) with this file.
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: '}'
config file:
<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>
And all went fine. Here you have a 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.
All looks fine. Are you sure you have a correct targetFiles? **/*.config
means that extension will look for any-folder-name/any-file-name.config
located in your root folder.
Solution 2:[2]
The error replaced 0 tokens out of 0 in 0 file(s) in 0.04 seconds
indicates the config or json files were not found in the default working directory.
Below are the possible reasons that the config/json files not found in the default working directory.
1, Your artifacts published from build pipeline is zipped. If the config/json files reside in the zipped artifacts, they cannot be found by the replacetoken task.
So you need to check if the artifacts downloaded in release pipeline is a zip file. If it is zipped, you need to add the Extract files task to unzip it before replacetoken task.
You can easily check the artifacts by clicking the 3dots highlighted in below screenshot.
2, If the config/json files are not in the default working directory. You need to specify in Root directory field the path to the folder where the config/json files reside.
Hope above helps!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Levi Lu-MSFT |