This GitHub project has been archived. Ongoing development on this project can be found in https://github.com/qetza/replacetokens-task.
Azure Pipelines extension that replace tokens in text files with variable values.
If you are using the UI, add a new task, select Replace Tokens from the Utility category and configure it as needed:
If your are using a YAML file, add a task with the following syntax:
- task: qetza.replacetokens.replacetokens-task.replacetokens@3
displayName: 'Replace tokens'
inputs:
targetFiles: |
**/*.config
**/*.json => outputs/*.json
Note: the task will only work on text files, if you need to replace token in archive file you will need to first extract the files and after archive them back.
The parameters of the task are described bellow, in parenthesis is the YAML name:
Root directory (rootDirectory): the base directory for searching files. If not specified the default working directory will be used. Default is empty string
Target files (targetFiles): the absolute or relative newline-separated or comma-separated paths to the files to replace tokens. Wildcards can be used (eg: **\*.config
for all .config files in all sub folders). Default is **/*.config
Syntax: {file path}[ => {output path}]
web.config
will replace tokens in web.config and update the file.web.tokenized.config => web.config
will replace tokens in web.tokenized.config and save the result in web.config.config\web.tokenized.config => c:\config\web.config
will replace tokens in config\web.tokenized.config and save the result in c:\config\web.config.Wildcard support
*.tokenized.config => *.config
will replace tokens in all {filename}.tokenized.config target files and save the result in {filename}.config.**\*.tokenized.config => c:\tmp\*.config
will replace tokens in all {filename}.tokenized.config target files and save the result in c:\tmp\{filename}.config.Only the wildcard * in the target file name will be used for replacement in the output.\ Relative paths in the output pattern are relative to the target file path.\
Negative pattern\ If you want to use negative pattern in target file, use a semi-colon
;
to separate the including pattern and the negative patterns. When using output syntax, only the wildcard in the first pattern will be used for generating the output path.
**\*.tokenized.config;!**\dev\*.config => c:\tmp\*.config
will replace tokens in all {filename}.tokenized.config target files except those under a dev directory and save the result in c:\tmp\{filename}.config.
Files encoding (encoding): the files encoding used for reading and writing. The 'auto' value will determine the encoding based on the Byte Order Mark (BOM) if present; otherwise it will use ascii. (allowed values: auto, ascii, utf-7, utf-8, utf-16le, utf-16be, win1252 and iso88591). Default is auto
Write unicode BOM (writeBOM): if checked writes an unicode Byte Order Mark (BOM). Default is true
Escape type (escapeType): specify how to escape variable values. Value auto
uses the file extension (.json
and .xml
) to determine the escaping and none
as fallback. Default is auto
(allowed values: auto, none, json, xml and custom)
Escape character (escapeChar): when using custom
escape type, the escape character to use when escaping characters in the variable values.
Characters to escape (charsToEscape): when using custom
escape type, characters in variable values to escape before replacing tokens.
Verbosity (verbosity): specify the level of log verbosity. Default is normal
(note: error and system debug are always on) (allowed values: normal, detailed and off)
Action on missing variable (actionOnMissing): specify the action to take on a missing variable. Default is warn
Keep token for missing variable (keepToken): if checked tokens with missing variables will not be replaced by empty string. Default is false
Action on no file processed (actionOnNoFiles): specify the action when no file was processed. Default is continue
(allowed values: continue, warn, fail)
Token pattern (tokenPattern): specify the pattern of the tokens to search in the target files. Default is default
(allowed values: default, rm, octopus, azpipelines, doublebraces and custom)
Token prefix (tokenPrefix): when using custom
token pattern, the prefix of the tokens to search in the target files. Default is #{
Token suffix (tokenSuffix): when using custom
token pattern, the suffix of the tokens to search in the target files. Default is }#
Use legacy pattern (useLegacyPattern): if checked whitespaces between the token prefix/suffix and the variable name are not ignored. Default is false
Use legacy empty/default feature (useLegacyEmptyFeature): if check use the old empty & default values features. The new feature/code can now distinguish between an undefined variable and an defined empty variable without the use of a specific "empty" token (Empty value parameter). Default is true
Empty value (emptyValue): with legacy empty/default feature: the variable value that will be replaced with an empty string. Default is (empty)
Use default value (useDefaultValue): with new empty/default feature: if check replace variable not found with a default value specified in Default value. Default is false
Default value (defaultValue): the value to be used if a variable is not found. With legacy empty/default feature: do not set to disable default value feature with the legacy feature; to replace with an empty string set the default value to the Empty value. Default is an empty string
Enable transformations (enableTransforms): if checked transformations can be applied on variable values. Default is false
. The following transformations are available:
#{lower(MyVar)}#
#{upper(MyVar)}#
#{noescape(MyVar)}#
#{base64(MyVar)}#
2
) and the second is a boolean specifying if the first line must be indented also (default false
). Examples #{indent(MyVar)}#
, #{indent(MyVar, 4, true)}#
Transform prefix (transformPrefix): The prefix between transform name and token name. Default is (
.
Transform suffix (transformSuffix): The suffix after the token name. Default is )
.
Variable files (JSON or YAML) (variableFiles): the absolute or relative comma or newline-separated paths to the files containing additional variables. Wildcards can be used (eg: vars\**\*.json
for all .json files in all sub folders of vars). YAML files must have the .yml
or .yaml
extension otherwise the file is treated as JSON. Variables declared in files overrides variables defined in the pipeline.
Inline variables (YAML syntax) (inlineVariables): A YAML formatted string containing inline variables. Variables declared inline overrides variables defined in the pipeline and in files.
Variable separator (variableSeparator): the separtor to use in variable names for nested objects and arrays in inline variables or variable files. Default is .
. Example: { 'My': { 'Value': ['Hello World!'] } }
will create a variable My.Value.0 with the value Hello World!.
Enable recursion in values (enableRecursion): if checked token replacement (with transformation) will be run on values.Example: "Say: #{value1}#" with value1 = "hello #{upper(value2)}#" and value2 = "world!" will result in "hello WORLD!". Default is false
Send anonymous usage telemetry (enableTelemetry): if checked anonymous usage data (hashed collection and pipeline id, no file parameter values, no variable values) will be sent to the task author only to analyze task usage. Default is true
The task creates the following as output variables:
The Replace Tokens task for Azure Pipelines collects anonymous usage data and sends them to its author to help improve the product by default. If you don’t wish to send usage data, you can change your telemetry settings through Send anonymous usage telemetry parameter or by setting a variable or environment variable REPLACETOKENS_DISABLE_TELEMETRY
to true
.
If you want to use tokens in XML based configuration files to be replaced during deployment and also have those files usable for local development you can combine the Replace Tokens task with the XDT tranform task:
New in 4.6.0
indent
transformation with indent size and indent first line parameters (326).indent
transformation with indent size and indent first line parameters (326).indent
transformation with indent size and indent first line parameters (326).New in 4.5.0
New in 4.4.1
New in 4.4.0
New in 4.3.0
New in 4.2.1
New in 4.2.0
New in 4.1.0
New in 4.0.0
custom
)New in 3.7.1
New in 3.7.0
.yml
or .yaml
extension (#177).New in 3.6.0
false
. New in 3.5.2
New in 3.5.1
System.ServerType
is not defined (#147).New in 3.5.0
New in 3.4.1
/
(#138)New in 3.4.0
New in 3.3.1
;
as a separator instead of new-line in Target files.New in 3.3.0
New in 3.2.2
New in 3.2.1
New in 3.2.0
auto
in File encoding (#99).auto
to Escape type and set it as default value.New in 3.1.0
system.debug
.New in 3.0.0
custom
in Escape values type parameter.Windows 1252
and ISO 8859-1
encoding to File encoding (#67)New in 2.3.0
New in 2.2.1
New in 2.2.0
New in 2.1.0
New in 2.0.2
New in 2.0.0
New in 1.4.1
New in 1.4.0
New in 1.3.1
New in 1.3.0
New in 1.2.0