Azure Pipelines use variable templates in a for loop

Melasecca, Greg 1 Reputation point
2021-12-06T23:12:30.313+00:00

I am new to Azure pipelines and I am having trouble understanding how I can make a loop work where I need to import variables based on the parameters I pass before runtime.

normally this seems easy and can be done if I use the below example_1 but when i have multiple parameter objects that i need to deploy to (i may have close to 100 at times) this means i have to execute this same pipeline that many times with x amount of 'regions':

example_1:

parameters:

  • name: region displayName: What Region? type: string default: north_america values:
    • north_america
    • south_america

variables:

  • template: path_to_file/${<!-- -->{ region }}.yml

stages:

  • stage: Deployment pool: ${<!-- -->{ variables.pool }} jobs:
  • template: azure-pipeline-job-1.yml
  • template: azure-pipeline-job-2.yml parameters: namespace: ${<!-- -->{ variables.namespace }}

But what if I need to loop through my regions object and based on the value that is passed into 'regions', that is what variables I need to use inside of EACH job within the stage its defined. for example_2 and the global.yml and regional specific .yml file.

parameters:

  • name: regions displayName: What Regions? type: object default:
    • north_america
    • south_america
    • asia_japan

not used variables anymore because they need to be dynamiclly generated inside of the loop

*variables:

  • template: regions/global.yml*

stages:

  • stage: One jobs:
    • ${<!-- -->{ each region in parameters.regions }}:
      • job: A_${<!-- -->{ region }} variables:
        • template: regions/global.yml
        • template: regions/${<!-- -->{ region }}.yml
        • template: environments/${<!-- -->{ region }}-${<!-- -->{ parameters.deployment_environment }}.yml steps:
          • task: Bash@3 displayName: Run job A inputs: targetType: inline script: | echo "specific environment variable: ${<!-- -->{ variables.environment }}" echo "specific namespace variable: ${<!-- -->{ variables.namespace }}" echo "specific endpoint variable: ${<!-- -->{ variables.endpoint }}"
      • job: B_${<!-- -->{ region }} steps:
        • task: Bash@3 displayName: Run job B inputs: targetType: inline script: | echo "specific environment variable: ${<!-- -->{ variables.environment }}" echo "specific namespace variable: ${<!-- -->{ variables.namespace }}" echo "specific endpoint variable: ${<!-- -->{ variables.endpoint }}"

the output of job A:

dev region_namespace global_endpoint

The issue is now I have to redefine all of those variables again if I want to run them in JOB B or JOB C.

regions/global.yml variables: environment: prod namespace: global_namespace endpoint: global_endpoint

regions/region.yml variables: environment: dev namespace: region_namespace endpoint:

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,524 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Vaibhav Chaudhari 38,651 Reputation points
    2021-12-07T06:55:50.63+00:00

    Hello,

    Azure DevOps related questions are not supported on this forum. It's better to reach out to experts in a dedicated forum over here:

    https://developercommunity.visualstudio.com/spaces/21/index.html


    Please don't forget to Accept Answer and Up-vote if the response helped -- Vaibhav

    1 person found this answer helpful.
    0 comments No comments