azure-devopsazure-pipelinesazure-pipelines-yaml

Variable Syntax in Azure DevOps YAML Pipeline with Each Loop


I am trying to figure out the syntax to be able to reference a variable within an each loop and replace it with the loop variable.

Here is a very simple example of what I am trying to do.

trigger:
  branches:
    include:
      - develop

variables:
  - name: regions
    value: ncus,scus
  - name: webAppName
    value: myapp-<region>

stages:
- stage:
  jobs:
  - ${{ each region in split(variables.regions, ',') }}:
    - job:
      pool:
        vmImage: 'windows-latest'
      steps:
      - script: 'echo "replace(webAppName, <region>, region)"'

What I want is the output of the echo statement in the script task to be myapp-ncus and myapp-scus. I know the syntax I have here is incorrect, I just wanted to give some pseudocode representation of what I am trying to do. I've tried all combinations of $(), ${{ }}, $[], but nothing seems to work.

Edit 1

Let me clarify the issue. The trouble I am having is specifically around the replace operation and trying to reference the loop variable in that replace operation. I know I can very easily reference the loop variable like this:

- script: 'echo ${{ region }}'

But I need to do a string replacement in the webAppName variable with the region loop variable. Here is a terrible attempt that doesn't work:

- script: 'echo ${{ replace(variables['webAppName'], '<region>', ${{ region }}) }}'

The issue I am having trouble with is doing the replace in a pipeline variable with the loop variable.


Solution

  • Example 1 - using replace function

    trigger: none
    
    pool:
      vmImage: 'ubuntu-latest'
    
    variables:
      - name: regions
        value: ncus,scus
    
      - name: webAppName
        value: myapp-<region>
    
    stages:
    - stage:
      jobs:
      - ${{ each region in split(variables.regions, ',') }}:
        - job: ${{ region }}
          variables:
            - name: webApp
              value: ${{ replace(variables.webAppName, '<region>', region) }}
          steps:
            - checkout: none
            - script: |
                echo "Deploying to $(webApp)"
              displayName: Deploy to $(webApp)
    

    Running the pipeline:

    Pipeline run using replace function

    Example 2 - using an app name prefix

    trigger: none
    
    pool:
      vmImage: 'ubuntu-latest'
    
    variables:
      - name: regions
        value: ncus,scus
    
      - name: webAppPrefix
        value: myapp-
    
    stages:
    - stage:
      jobs:
      - ${{ each region in split(variables.regions, ',') }}:
        - job: ${{ region }}
          variables:
            - name: webApp
              value: '${{ variables.webAppPrefix }}${{ region }}'
          steps:
            - checkout: none
            - script: |
                echo "Deploying to $(webApp)"
              displayName: Deploy to $(webApp)
    

    Running the pipeline:

    Pipeline run - using variable prefix