Unrecognized value: 'matrix' in Azure Devops Template - Stack Overflow

admin2025-05-01  0

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!

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!

Share Improve this question edited Jan 2 at 20:17 Rui Jarimba 18.3k11 gold badges64 silver badges98 bronze badges asked Jan 2 at 19:41 BanoonaBanoona 5641 gold badge5 silver badges9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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)
转载请注明原文地址:http://anycun.com/QandA/1746099980a91665.html