Enhance Azure DevOps: Manage Environments with Replace Tokens

Introduction

Every DevOps Engineer is responsible for writing environment-agnostic code, which means that the 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 template parameters using Replace Tokens. You can use the same technique for any requirements where you would like to dynamically change the values.

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.

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 marketplace 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 that contain values that are specific to three different environments as shown below.

Variable groups

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.

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 started. 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.

YAML Task Assistant

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 the PRD environment.

PRD environment

Summary

In this article, we have learned how to use the 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.


Similar Articles