Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure DevOps: An error occurred while loading the YAML build pipeline. An item with the same key has already been added

Lately I've been trying to make an Azure DevOps pipeline to deploy to 3 environments, with 2 different data centers and 2 different service connections. I've been trying to achieve this with using as little lines of YAML as possible.

After a lot of trial and error, I'm stuck on this message "An error occurred while loading the YAML build pipeline. An item with the same key has already been added."

deploy-env.yaml:

parameters:
  - name: OPENSHIFT_NAMESPACE
    displayName: 'OpenShift namespace'
    type: object
    values: []

  - name: DCR
    displayName: 'Data Center'
    type: object
    values: []

  - name: OSC
    displayName: 'Openshift service connection'
    type: object
    values: []

stages:
- ${{ each namespace in parameters.OPENSHIFT_NAMESPACE }}:
    - ${{ each dcr in parameters.DCR }}:
      - ${{ each osc in parameters.OSC }}:
        - stage: deploy-${{ convertToJson(namespace) }}
          jobs:
          - deployment: deploy_to_dcr
            environment: ${{ namespace }}
            displayName: 'Deploy to DCR1'
            strategy:
              runOnce:
                deploy:
                  steps:
                    - template: steps/deploy_to_cluster_with_helm_templating.yml@pipeline_templates
                      parameters:
                        DATA_CENTER: ${{ dcr }}
                        OPENSHIFT_NAMESPACE: ${{ namespace }}
                        OPENSHIFT_SERVICE_CONNECTION: '${{ osc }}'
                        HELM_VALUES:
                          - 'global.namespace=${{ namespace }}'
                          - 'global.data_center=${{ DCR }}'

azure-pipeline.yaml

  resources:
  repositories:
    - repository: pipeline_templates
      type: git
      name: pipeline-templates


stages:
  - template: deploy-env.yaml
    parameters:
      OPENSHIFT_NAMESPACE:
        - development
        - test
        - acceptance
      DCR:
        - dcr1
        - dcr2
      OSC:
        - OCP4DCR1
        - OCP4DCR2

Does anyone knows why this error occurs? I've found other articles where stage/job names we're not unique, but that is not the case in this example.

Thanks in advance.

like image 839
Tony Avatar asked Sep 19 '25 16:09

Tony


1 Answers

This line is getting repeated twelve times, with only four different values:

- stage: deploy-${{ convertToJson(namespace) }}

Stage names must be unique.

like image 52
Vince Bowdren Avatar answered Sep 23 '25 05:09

Vince Bowdren