3

I have a Azure DevOps YAML Pipeline which has multiple deploy stages and I want to template the stages and add these using a for-each template loop, this is what I have so far

parameters:
  - name: Deployments
    type: object
    default:
      Deployment:
        name: DeployDev
        displayName: ...
        dependsOn: ...
        environment: ...
        ...

stages:
  - stage: SomeOtherStage
    ...

  - ${{ each deployment in parameters.Deployments }}
    - template: ./path/to/template/file.yml
      parameters:
        name: ${{ deployment.name }}
        displayName: ...
        ...

And in my stage template

parameters:
  - name: name
    type: string
  - name: displayName
    ...

stages:
  - stage: ${{ parameters.name }}
    displayName: ${{ parameters.displayName }}
    ...

My issue is that when I get this into Azure DevOps and validate the pipeline I get the validation error /azure-pipelines-cd.yml: (Line: 36, Col: 15, Idx: 685) - (Line: 36, Col: 15, Idx: 685): Mapping values are not allowed in this context..

I have seen examples of this done elsewhere but cannot see why I am getting this error, has anyone else done this and got it working

Neil Stevens
  • 3,534
  • 6
  • 42
  • 71
  • 1
    Had the same error caused missing the colon at the end, in your case it seems to be in `${{ each deployment in parameters.Deployments }}`. Maybe with colon at the end (`${{ each deployment in parameters.Deployments }}:`) it works as it was originally? – patriml Jul 13 '22 at 14:29

2 Answers2

3

Stage doesn't support "each" function. To use Stage templates, you may refer to the following sample. In this example, a stage is repeated twice for two different testing regimes. The stage itself is specified only once.:

# File: stages/test.yml

parameters:
  name: ''
  testFile: ''

stages:
- stage: Test_${{ parameters.name }}
  jobs:
  - job: ${{ parameters.name }}_Windows
    pool:
      vmImage: vs2017-win2016
    steps:
    - script: npm install
    - script: npm test -- --file=${{ parameters.testFile }}
  - job: ${{ parameters.name }}_Mac
    pool:
      vmImage: macos-10.14
    steps:
    - script: npm install
    - script: npm test -- --file=${{ parameters.testFile }}

# File: azure-pipelines.yml

stages:
- template: stages/test.yml  # Template reference
  parameters:
    name: Mini
    testFile: tests/miniSuite.js

- template: stages/test.yml  # Template reference
  parameters:
    name: Full
    testFile: tests/fullSuite.js
Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39
  • I'll accept this as the answer for anyone else finding this question as this is pretty much what I went with anyway while waiting in this question – Neil Stevens Dec 17 '20 at 11:47
  • Stage does support `each`. At least currently. The problem was likely caused by a missing colon, as mentioned in the comment to the question itself. – n0rd Jun 03 '23 at 02:45
0

While not relevant to the OP's question, this error can also be caused by writing -job instead of - job (note the lack of the space between - and job in the former).

derekbaker783
  • 8,109
  • 4
  • 36
  • 50