Colin's ALM Corner Build Tasks - Replace Tokens
This task invokes replaces tokens in a file with values from the matching variables set in the Environment.
The task requires the following settings:
- Target File: path to the file that contains the tokens.
- Token Regex: a RegEx to find the tokens. Must include a group selector. This defaults to
__(\w+)__. This will match tokens that have double-underscore
__prefix and postfix (e.g.
The environment variables should use the name of the token without the token identifiers. They can be defined in the Release (global - applies to all Environments) or in an Environment (in the variables set for the Environment).
Imagine you have a file with the following contents:
<?xml version="1.0" encoding="utf-8"?> <parameters> <setParameter name="IIS Web Application Name" value="__SiteName__" /> </parameters>
This file contains a token called "SiteName".
Drop the Task into the build or release, and then set the Target File to the path where the file is. Then set a global variable or an environment variable called "SiteName" and give it the value you want the token to be replaced with. That's it!
If you wish to recurse through subdirectories of the Source Path, set the Target File Pattern to **\MyFileRegex
Different Token Identifiers
If your tokens have a different identifier, then you can change the Token Regex. For example, if your tokens
are of the form
--Token--, then you can change the Regex to
--(\w+)-- and the task will work.
Existing environment variables.
Be aware that there may be some existing environment variables on the build/release agent. For example
Usernameis set to the identity that the agent is running under. In order to avoid conflicts, you should ensure that any tokens have a unique name.
WebUsernameis a better token than
Usernamefor this reason. You can see all the environment variables in the logs for a deployment.
Since the native vso-task-lib does not support secrets for Node (it does for PowerShell) there is a hack that allows you to specify secrets as an advanced parameter. You specify them in key-value pairs (with the key being the name and the value being the secret variable) and can use a semi-colon to separate them:
Using Tokenizer with ReplaceTokens
It is expected that this combination will be used for DotNet Core applications. You will likely want to tokenize the appsettings.json file during the build and then use the ReplaceTokens task to fill in values during the Release. This is possible, but you will need to change the defaults for the ReplaceTokens task in order to work with the json "namespaces". The following process will get you going:
- Use the Tokenizer to tokenize the appsettings.json file as described above.
- On the Release, enter the name of the tokens but substitute an
_(underscore) for the
.(period). Using the above example, you'd need three environment variables:
- On the Release, add a ReplaceTokens task and change the default Token Regex parameter to