Friday 30 September 2022

Replace String in Variable in Azure DevOps YAML Pipelines

In  Azure DevOps YAML pipelines there are several functions available for you to use. replace is such a useful function, which you can use to replace string segements within a string. Let's look at how we can use replace function to replace contents of a variable. 

Use case would be: For example you have a variable value for your customer api name as cutomer-api.  You may need to use it and generate customer_api value for another variable which you want to use in a namig of resouce, for consitency purpose with _ in naming instead of -.

For this purpose you can use replace function as shown below. The - in variable apicontainerurlprefix will be replaced with _ and will be assigned to apicontainer name variable.

  apicontainername: $[replace(variables['apicontainerurlprefix'], '-', '_')]

Test pipeline to show usage is below.


    apicontainername$[replace(variables['apicontainerurlprefix'], '-', '_')]
    - checkoutnone
    - scriptecho "apicontainerurlprefix is:" $(apicontainerurlprefix)
              "apicontainername is:" $(apicontainername)

When you execute the pipeline above, its log shows how the replace function has replaced - with _.

No comments:

Popular Posts