Improve Azure DevOps: Handle Environments with Substitute Tokens
Introduction
Each DevOps Engineer is liable for writing environment-agnostic code, which signifies that the code that’s written ought to work in any setting (DEV/TEST/PREPROD/PROD). Nonetheless, the configuration values may change throughout environments. On this article, we’re going to learn to dynamically change the environment-specific values within the Azure DevOps Pipelines utilizing an Azure DevOps Extension referred to as Substitute Tokens.
Within the beneath use case, we’re going to learn to exchange the ARM template parameters utilizing Substitute Tokens. You should use the identical approach for any necessities the place you wish to dynamically change the values.
Use Case
Beneath is a quite simple JSON file (named Tip10.parameters.json), which is used to go Configuration values (additionally referred to as Parameters recordsdata in ARM Templates).
{
"$schema": "https://schema.administration.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"pAppServicePlanName": {
"worth": "az-devops-dev-eus-asp1"
},
"pAppServiceName": {
"worth": "az-devops-dev-eus-wapp1"
},
"pAppInsightsName": {
"worth": "az-devops-dev-eus-wapp1-ai"
},
"pSQLServerName": {
"worth": "az-devops-dev-eus-sqlserver1"
}
}
}
These Configuration values have to be environment-specific, they usually have completely different values in numerous environments. DevOps engineers must develop the Azure DevOps pipelines which ought to exchange these values simply earlier than executing the ARM Templates.
With a purpose to obtain that, we have to use tokens as an alternative of hardcoding the names of the providers. Let’s exchange the parameter values as proven beneath
{
"$schema": "https://schema.administration.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"pAppServicePlanName": {
"worth": "#{pAppServicePlanName}#"
},
"pAppServiceName": {
"worth": "#{pAppServiceName}#"
},
"pAppInsightsName": {
"worth": "#{pAppInsightsName}#"
},
"pSQLServerName": {
"worth": "#{pSQLServerName}#"
}
}
}
Azure DevOps Extensions – Substitute Tokens
As mentioned on this article, we’re going to leverage a market extension referred to as Substitute Tokens, which have to be put in into your Azure DevOps Group. You may obtain the extension from the Azure DevOps Marketplace for Free at Azure DevOps – Substitute Tokens
Azure DevOps Pipelines – Variable Group
Now, Let’s create three variable teams that include values which are particular to 3 completely different environments as proven beneath.
Azure DevOps Pipelines – Parameter file
Our purpose is to exchange the values of the Parameters file named Tip10.parameters.json dynamically utilizing the values accessible within the corresponding Var Group.
Azure DevOps Pipelines – YAML Pipeline
Let’s now create a brand new YAML Pipeline utilizing the beneath steps.
- Checkout the repo
- Add the ReplaceTokens duties
- Publish the Tip10.parameters.json
Let’s get began. Create a brand new Pipeline and add the beneath code to your pipeline.
variables:
- group: Tip10-PRD #Change it primarily based on the setting
steps:
- checkout: self
- activity: replacetokens@5
inputs:
rootDirectory: '$(System.DefaultWorkingDirectory)'
targetFiles: '**/*.json'
encoding: 'auto'
tokenPattern: 'default'
writeBOM: true
actionOnMissing: 'warn'
keepToken: false
actionOnNoFiles: 'proceed'
enableTransforms: false
enableRecursion: false
useLegacyPattern: false
enableTelemetry: true
- activity: PublishBuildArtifacts@1
inputs:
PathtoPublish: '$(System.DefaultWorkingDirectory)/Ideas/Tip10.parameters.json'
ArtifactName: 'Tip10-Recordsdata'
publishLocation: 'Container'
You may add the Substitute Tokens step utilizing the YAML Job Assistant, as proven beneath.
Alright, as soon as you’re prepared with the code within the pipeline you may execute the pipeline which creates the artifact which comprises the values for the particular setting. Beneath is the way it seems for the PRD setting.
Abstract
On this article, we have now realized how you can use the Substitute Tokens activity to dynamically use Surroundings-specific variables from Azure DevOps Variable Teams.
On this instance, we have now changed the values of an ARM Template parameter file. Nonetheless, you should use this identical approach for any file.
Thanks for studying.
Know extra about our firm at Skrots. Know extra about our providers at Skrots Companies, Additionally checkout all different blogs at Weblog at Skrots