I’ll explain how to replace app config settings in Azure DevOps with the Replace Tokens extension to dynamically change config variables.
Some applications require different configurations for a test stage and a production stage for example. When using Azure DevOps, we can dynamically change configuration variables in a build pipeline using the Replace Tokens extension. I’ll show you how to set it up and the needed configuration steps. Let’s see how to replace app config settings in Azure DevOps.
Defining placeholders in the config file
Let’s assume, we have a .NET application with an appsettings.json file containing some configuration values. Here is a small example:
There are 4 keys and their values already contain placeholders enclosed with #{…}
. These values will be replaced by our pipeline during each run.
Installing the extension
The Replace Tokens extension adds a task that you can include in your pipeline. Click on Get it free, sign in with your Azure DevOps account and wait for the installation process to finish. It will be available in all of your projects of your organization.
Want More Flutter Content?
Join my bi-weekly newsletter that delivers small Flutter portions right in your inbox. A title, an abstract, a link, and you decide if you want to dive in!
Defining the variables
There are two ways to define variables. We can define them inside the YAML file or we can create a variable group and reference it in the YAML file.
variables:
- group: configGroup
- name: CollectArticles
value: true
- name: SendNewsletter
value: false
- name: UseProdMode
value: false
- name: FeedUrl
value: https://xeladu.medium.com/feed
The code above needs to be inserted into your pipeline file at the root level (more details on variable scopes can be found HERE). It contains a group definition and individual variable definitions. In that case, the last set value wins if identical variable names are used. You can find more details about setting variables in pipelines HERE.
To define a variable group, go to Library → Variable groups and click on + Variable group. An editor will appear that allows defining key-value pairs. The name of the group needs to be referenced in the YAML file. Here is an example screenshot:
Enhancing the build pipeline
Add the following code to the beginning of your pipeline:
- task: replacetokens@5
displayName: Replace tokens in configuration file
inputs:
targetFiles: 'PATH-TO-YOUR-FILE(S)'
encoding: 'auto'
tokenPattern: 'octopus'
writeBOM: true
actionOnMissing: 'fail'
keepToken: false
actionOnNoFiles: 'fail'
enableTransforms: false
enableRecursion: false
useLegacyPattern: false
enableTelemetry: true
This task will look for all target files (you can use a fixed path or work with wildcards to match multiple files), search for the defined token pattern in them, and replace the values with our custom ones.
There are multiple token patterns available to choose from. You can also use different patterns in different files so that each Replace Tokens task only transforms one file at a time. See the image below for pattern examples:
Result
When the pipeline runs, the extension replaces the tokens with the defined values. Your appsettings.json that is used by the application will look like this:
🔔 Hint
The value of SendNewsletter
is false because the inline setting in the YAML file overrides the group setting. Always watch your variables to prevent surprises!
Conclusion
I showed you how to replace app config settings in Azure DevOps to support multiple environments like test and production. The steps are easy to implement and the Replace Tokens extension is free to use.