top of page
Writer's pictureSumit Raj

LetsDevOps: How to use Replace token in Azure DevOps

Introduction

In this article we will learn how to use replace token in ADO pipeline. This can help to update the file with variable value during the pipeline execution.


Scenario to Use

  • Assume you have some file that needs to be updated before the pipeline starts like ARM template file with some custom value.

  • Any other configuration file with value specific to environment.

  • File like web.config that needs to be updated.

Supported Data Type

  • String

  • Number


Setup Workflow

Step 1: Determine which config/json or any other file you want to use for replacing the token.


Step 2: Tokenize all required variable in file like below.


Example: I have one demo json file.

{
    "ConnectionString": "#{ConnectionString}#",
    "WebAppURL": "#{WebAppURL}#",
    "VMCount": #{VmCount}#
}

Step 3: Install Replace Token Extension to Azure DevOps. Follow here.


Step 4: Once Extension installed you can see the Option while editing the Pipeline.


Pipeline --> Edit Pipeline --> Task --> Search for replace token.


Step 5: Now replace token step needs to be added in the Pipeline. You make sure to include this task before using the file.



- task: replacetokens@5
  inputs:
    targetFiles: '$(System.DefaultWorkingDirectory)/Profile/demo.json'
    encoding: 'auto'
    tokenPattern: 'default'
    writeBOM: true
    actionOnMissing: 'warn'
    keepToken: false
    actionOnNoFiles: 'continue'
    enableTransforms: false
    enableRecursion: false
    useLegacyPattern: false
    enableTelemetry: true

Step 6: Declare and Define the variable.


We can define the variable multiple way.

  • Pipeline variable

  • Variable Group


I have defined as pipeline variable.

variables:
  VMCount: ${{parameters.VMCount}}
  WebAppURL: "https://www.letsdevops.net/"
  ConnectionString: "Database Sourc=Demo"

Step 7: Demo YAML file for your reference.



name: yamldemo_$(Date:yyyyMMdd)$(Rev:.r)
parameters:

  - name: VMCount
    displayName: "Enter the VM Count"
    type: number

variables:
  VMCount: ${{parameters.VMCount}}
  WebAppURL: "https://www.letsdevops.net/"
  ConnectionString: "Database Sourc=Demo"

trigger: none
pool:
 name: default
steps:

- task: replacetokens@5
  inputs:
    targetFiles: '$(System.DefaultWorkingDirectory)/Profile/demo.json'
    encoding: 'auto'
    tokenPattern: 'default'
    writeBOM: true
    actionOnMissing: 'warn'
    keepToken: false
    actionOnNoFiles: 'continue'
    enableTransforms: false
    enableRecursion: false
    useLegacyPattern: false
    enableTelemetry: true
- task: PowerShell@2
  displayName: "Display Updated token File"
  inputs:
    targetType: 'inline'
    script: |
      # Write your PowerShell commands here.
      Write-Host "Displaying the Content"
      Get-Content -path $(System.DefaultWorkingDirectory)/Profile/demo.json
      
      

Step 8: Run the pipeline and view the result.


Step 9: As we see the result content updated as per the data types.


You can use this technique as per your requirement.








Comentarios


bottom of page