Bryce Howitson
Bryce Howitson

Reputation: 7710

Duplicate Keys error in Azure Devops Pipeline YAML when using if/else

I'm trying to run a different build mode depending on the target branch in an Azure Devops pipeline so I created a variable and an if/ese.

According to this MS Documentation, it appears that I'm doing things correctly. My IDE that does syntax/error checking doesn't complain about the YAML.

However, when I try to run the build pipeline, I get "Duplicate Keys" as an error for the buildScript variable. Does anyone know a way around this? Or am I just doing something silly in my YAML?

I was under the impression duplicate keys were allowed in YAML.

variables:
  slotTarget: 'development'
  ${{ if eq(variables['Build.SourceBranchName'], 'main') }} :
    buildScript: build
  ${{ else }} :
    buildScript: devBuild

jobs:
- job: buildApp
  displayName: 'Build the Vue App'
  #Set ENV.VARS for Webpack to read and inject during build
  - bash: |
      echo Running build for $BUILDNUM
      BUILDNUMBER=$BUILDNUM npm run $(buildScript)
    displayName: 'Create Build - $(build.buildNumber) for GIT $(build.SourceVersion) Trigger: $(build.Reason)'
    env:
      BUILDNUM: $(build.buildNumber)

Edit: I'm starting to suspect that there are different versions of the Azure DevOps pipeline parser. Maybe based on subscription age? Seems like the same code works for some people and not others. Can anyone verify this?

Upvotes: 5

Views: 4389

Answers (2)

rick
rick

Reputation: 472

I tried another method today and it works for me.

Use following:

- name: buildScript
  ${{ if eq(variables['Build.SourceBranchName'], 'main') }} :
    value: build
  ${{ else }} :
    value: devBuild

Doing so in YAML in Azure Pipeline browser will still highlight the both value as duplicate key however when you try to validate (from the ellipses/top right three dots) or run it, there's no issue. Not entirely sure why this does not work.

Note: tried in both West Europe and Southeast Asia region, they worked fine.

Upvotes: 2

Bowman Zhu
Bowman Zhu

Reputation: 7241

This is the YAML I run on my side:

variables:
  slotTarget: 'development'
  ${{ if eq(variables['Build.SourceBranchName'], 'main') }} :
    buildScript: build
  ${{ else }} :
    buildScript: devBuild

jobs:
- job: buildApp
  displayName: 'Build the Vue App'
  #Set ENV.VARS for Webpack to read and inject during build
  steps:
  - task: CmdLine@2
    inputs:
      script: |
        echo Write your commands here
        
        echo $(buildScript)

No error output.

If the pipeline run based on the main branch, it returns: enter image description here If the pipeline run not based on the main branch, it returns: enter image description here

I think the expression you are using is correct. Could you please test the above pipeline YAML definition on your side and let me know whether still have some error outputs.

If still have the error, since StackOverflow is an open community, I recommend you to post questions on the Developer Community of Microsoft so that we can check the debug logs.

Upvotes: 0

Related Questions