Object reference not set to an instance of an object in azure pipeline

Viewed 7952

I'm trying to import variable groups depending on the current branch in azure build pipeline, but I get this error: 'Object reference not set to an instance of an object'.

I have simplified the case and I get this error when I have both of the lines (condition and import) in my .yaml file:

variables:
 ${{ if eq('bbb', 'master') }}:
  - group: variables-shared

If I remove condition, everything works as expected. If I remove group importing, I get other errors related to undefined variable below (that is normal).

I am interested why I get this error

enter image description here

5 Answers

I also had this exact issue. The reasoning in the currently accepted answer is not correct, and you can in fact use conditional insertion in your azure-pipelines.yml file to conditionally include a particular variable group.

When the docs say that conditional insertion requires the use of template syntax, they're referring to template expression syntax, not the use of templates directly. Per the previous link, template expression syntax is the ${{ }} syntax for expanding expressions.

As gleaned from this example from the docs, The problem with the example in the question is actually a syntax error.

Incorrect:

variables:
 ${{ if eq('bbb', 'master') }}:
  - group: variables-shared

Correct:

variables:
 - ${{ if eq('bbb', 'master') }}:
   - group: variables-shared

Note the leading - before the $ char on the second line. I've tested this in my own pipelines and it works, although it does freak out the yaml syntax checker in my IDE.

If I remove condition, everything works as expected. I am interested why I get this error

Check the Expressions doc and you will find this: Conditionals only work when using template syntax.

That's why you can not use condition for your variable group.

The workaround is to use the template to store your variables rather than variable groups.

Please refer to Variable reuse:

# File: vars.yml
variables:
  favoriteVeggie: 'brussels sprouts'


# File: azure-pipelines.yml

variables:
- template: vars.yml  # Template reference

steps:
- script: echo My favorite vegetable is ${{ variables.favoriteVeggie }}.

I found two other potential causes to "An error occurred while loading the yaml build pipeline. Object reference not set to an instance of an object."

  1. "stages:" was missing in stage template before the stage
  2. Template file reference didn't exist (- template: 'template.yml')

I actually discovered a hack yesterday for debugging this obnoxiously vague and wide-ranging error message.

For the build that fails with this message, if you click "Run new" and try to run the job manually by clicking "Run", it will typically give you a much more specific error message at that point.

For instance:

azure devops pipeline manual run fail

I had also an object reference not set to an instance of an object with code 606802. The build pipeline had no errors at all.

The error was caused by a pre-validation build, where 1 parameter value had no default value.

After adding the default value, the PR validation build succeeded.

Related