Search code examples
azure-devopsazure-pipelinesazure-pipelines-yaml

Unrecognized value: 'matrix' in Azure Devops Template


In Azure Devops templates, how do I reference a matrix variable from the parameters for a job template?

I have the following code:

pipeline.yml

  trigger:
  branches:
    include:
      - master
  paths:
    include:
      - my/code/lives/here/*  # Trigger only for changes under this directory
pr: none

variables:
  imagetag: 'v1.0.0'

stages:
- stage: Dev
  displayName: 'Development Environment'
  jobs:
    - job: DevRegions
      displayName: 'Environment Region: '
      pool:
        name: $[format('{0}-POOL', upper(variables.env_region))]  # Dynamically set pool name based on env region
      strategy:
        matrix:
          dev-us:
            env_region: 'dev-uswest'
          dev-eu:
            env_region: 'dev-euwest'
      steps:
        - template: templates/my-job-template.yml  
          parameters:
            env_region: ${{ matrix.env_region }}
            imagetag: $(imagetag)
    

my-job-template.yml

parameters:
  env_region: ''
  imagetag: ''

steps:
  - script: |
        echo ${{ parameters.env_region }}
    displayName: 'Echo env region'

This fails as below:

Unrecognized value: 'matrix'. Located at position 1 within expression: matrix.env_region. 

Additionally, it is able to generate a pool name successfully from the matrix! I now want to push env-region-specific parameters into the job template. How do I do this?

Thanks!


Solution

  • You can reference values from the matrix configuration using macro syntax $(xxx)

    Also, keep in mind that matrix configuration names must start with a letter and contain only basic Latin alphabet letters (A-Z and a-z), digits (0-9), and underscores (_).

    Try the following (not tested):

    # ...
    
    stages:
    - stage: Dev
      displayName: 'Development Environment'
      jobs:
        - job: DevRegions
          displayName: 'Environment Region: '
          pool:
            name: $(pool) # <------------ from matrix configuration
          strategy:
            matrix:
              dev_us: # <----------------- using underscore instead of dash
                env_region: 'dev-uswest'
                pool: 'DEV-USWEST-POOL' # <------------ agent pool
              dev_eu: # <----------------- using underscore instead of dash
                env_region: 'dev-euwest'
                pool: 'DEV-EUWEST-POOL' # <------------ agent pool
          steps:
            - template: templates/my-job-template.yml  
              parameters:
                env_region: $(env_region) # <------------ from matrix configuration
                imagetag: $(imagetag)