As DevOps Engineer, you are responsible for to develop the Azure DevOps pipelines which should replace these values (DEV/TEST/PREPROD/PROD) based on the environment. However, the configuration values could change across environments. In this article, we are going to learn how to dynamically change the environment specific values (Variable Substitution) in the Azure DevOps Pipelines using an Azure DevOps Extension called Replace Tokens.
In my previous article, we had discussed about DevOps guys need to ensure all the secrets need to be kept inside the Key vault instead of using directly from the Azure DevOps Variable group. But all the project decision will not be same, still many project using the variable group for maintaining the secrets and lock them. This article is focusing on same and will explain how to Manage environment specific configurations (Variable Substitution using Replace Tokens – Azure DevOps Marketplace extension
Example Config File
Below is the sample config file which we are going to use for variable substitution from Key Vault in YAML Azure DevOps pipelines
These Configuration values must be environment specific and they have different values in different environments. DevOps engineers would have to develop the Azure DevOps pipelines which should replace these values in my below config. In my case the smtp-host, smtp-username, smtp-password are different for lower environment (dev/qa & preprod) and higher environment.
How to use Replace Tokens Extension in Azure YAML pipeline
Here we are going to use Replace Tokens task to replace tokens in config files with variable values.
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 paths to the files to replace tokens. Wildcards can be used (eg: **\*.config for all .config files in all sub folders). Default is **/*.config
- 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 }#
Example 1: Replace with Target files parameter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
- task: replacetokens@5 displayName: 'replacing token in new config file' inputs: targetFiles: | src/Feature/dotnethelpers.Feature.Common.General.config src/Foundation/dotnethelpers.Foundation.SMTP.config encoding: 'auto' writeBOM: true actionOnMissing: 'warn' keepToken: false tokenPrefix: '{' tokenSuffix: '}' useLegacyPattern: false enableTelemetry: true |
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.
Example 2: Replace with Root directory and Target files parameter
You can able to see, we can also give the targe files with comma separate as well like below YAML.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
- task: replacetokens@5 inputs: rootDirectory: 'src/Feature/Forms/code/App_Config/Include/Feature/' targetFiles: 'dotnethelpers.Feature.config,dotnethelpers.Foundation.SMTP.config' encoding: 'auto' tokenPattern: 'default' writeBOM: true actionOnMissing: 'warn' keepToken: false actionOnNoFiles: 'continue' enableTransforms: false enableRecursion: false useLegacyPattern: false enableTelemetry: true |
Example 3: Replace with wildcard
Target files (targetFiles): the absolute or relative newline-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.
As per below targetfiles path, replace token will search all the .config files inside the node folder and replace the token if applicable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
- task: replacetokens@5 displayName: replacing token inputs: targetFiles: | node/*.config encoding: 'auto' writeBOM: true actionOnMissing: 'warn' keepToken: false tokenPrefix: '#{' tokenSuffix: '}#' useLegacyPattern: false enableTelemetry: true |
Leave A Comment