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: