Azure DevOps – Tips and Tricks – 10 – Manage environment specific configurations using Replace Tokens – Azure DevOps Marketplace extension

In this article, we are going to learn how to change the values that are specific for each environment using an Azure DevOps extension called Replace Tokens.

Advertisements

Introduction

Every DevOps Engineer is responsible for writing environment agnostic code which means that code that is written should work in any environment (DEV/TEST/PREPROD/PROD). However, the configuration values could change across environments. In this article, we are going to learn how to dynamically change the environment specific values in the Azure DevOps Pipelines using an Azure DevOps Extension called Replace Tokens.

In the below use case, we are going to learn how to replace the ARM Templates parameters using Replace Tokens. You can use the same technique for any requirements where you would like to dynamically change the values.

Advertisements

Use Case

Below is a very simple JSON file (named Tip10.parameters.json) which is used to pass Configuration values (also called Parameters files in ARM Templates).

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
         "pAppServicePlanName": {
            "value": "az-devops-dev-eus-asp1"
         },
         "pAppServiceName": {
            "value": "az-devops-dev-eus-wapp1"
         },
         "pAppInsightsName": {
            "value": "az-devops-dev-eus-wapp1-ai"
         },
         "pSQLServerName": {
            "value": "az-devops-dev-eus-sqlserver1"
         }
    }
}

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 just before executing the ARM Templates.

Advertisements

In order to achieve that, we need to use tokens instead of hardcoding the names of the services. Let’s replace the parameter values as shown below.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
         "pAppServicePlanName": {
            "value": "#{pAppServicePlanName}#"
         },
         "pAppServiceName": {
            "value": "#{pAppServiceName}#"
         },
         "pAppInsightsName": {
            "value": "#{pAppInsightsName}#"
         },
         "pSQLServerName": {
            "value": "#{pSQLServerName}#"
         }
    }
}

Azure DevOps Extensions – Replace Tokens

As discussed, in this article we are going to leverage a market place extension called Replace Tokens which must be installed into your Azure DevOps Organization. You can download the extension from the Azure DevOps Market for Free at Azure DevOps – Replace Tokens

Azure DevOps Pipelines – Variable Group

Now, Let’s create three variable groups which contains values that are specific to three different environments as shown below.

Azure DevOps – Tips and Tricks – 10 – Replace Tokens – Environment Variables

Azure DevOps Pipelines – Parameter file

Our goal is to replace the values of the Parameters file named Tip10.parameters.json  dynamically using the values available in the corresponding Var Group.

Advertisements

Azure DevOps Pipelines –YAML Pipeline

Let’s now create a new YAML Pipeline using the below steps.

  1. Checkout the repo
  2. Add the ReplaceTokens tasks
  3. Publish the Tip10.parameters.json

Let’s get stared. Create a new Pipeline and add the below code to your pipeline.

variables:
- group: Tip10-PRD #Change it based on the environment
steps:
    - checkout: self
    - task: replacetokens@5
      inputs:
        rootDirectory: '$(System.DefaultWorkingDirectory)'
        targetFiles: '**/*.json'
        encoding: 'auto'
        tokenPattern: 'default'
        writeBOM: true
        actionOnMissing: 'warn'
        keepToken: false
        actionOnNoFiles: 'continue'
        enableTransforms: false
        enableRecursion: false
        useLegacyPattern: false
        enableTelemetry: true
    - task: PublishBuildArtifacts@1
      inputs:
        PathtoPublish: '$(System.DefaultWorkingDirectory)/Tips/Tip10.parameters.json'
        ArtifactName: 'Tip10-Files'
        publishLocation: 'Container'

You can add the Replace Tokens step using the YAML Task Assistant as shown below.

Azure DevOps – Tips and Tricks – 10 – Replace Tokens – Task configuration

Alright, once you are ready with the code in the pipeline you can execute the pipeline which creates the artifact which contains the values for the specific environment. Below is how it looks for PRD environment.

Advertisements
Azure DevOps – Tips and Tricks – 10 – Replace Tokens – After replacement

Summary

In this article, we have learnt how to use Replace Tokens task to dynamically use Environment specific variables from Azure DevOps Variable Groups.

In this example, we have replaced the values of an ARM Template parameter file. However, you can use this same technique for any file.

Thanks for reading.

Do you like this article? If you want to get more updates about these kind of articles, you can join my Learning Groups

WhatsApp

Telegram

Advertisements
Advertisements
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s