Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 495
ABC Avatar asked Mar 07 '20 20:03

ABC


People also ask

How do you fix Object reference not set to an instance of an object?

To fix "Object reference not set to an instance of an object," you should try running Microsoft Visual Studio as an administrator. You can also try resetting the user data associated with your account or updating Microsoft Visual Studio to the latest version.

How do you set the environment on an azure pipeline?

Create an environmentSign in to your organization: https://dev.azure.com/{yourorganization} and select your project. Select Pipelines > Environments > Create environment. Enter information for the environment, and then select Create. Resources can be added to an existing environment later.

How do I use an Azure artifact in the pipeline?

Access your Azure DevOps project and click Project Settings > Pipeline > Service connections > New service connection and select JFrog Artifactory or JFrog Distribution. Configure the details of the JFrog Artifactory or JFrog Distribution instance.

How do you trigger a pipeline in Azure?

Stages filters for pipeline resource triggers requires Azure DevOps Server 2020 Update 1 or greater. You can trigger your pipeline when one or more stages of the triggering pipeline complete by using the stages filter. If you provide multiple stages, the triggered pipeline runs when all of the listed stages complete.


3 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.

like image 134
Ben Burns Avatar answered Oct 16 '22 08:10

Ben Burns


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 }}.
like image 1
Yang Shen - MSFT Avatar answered Oct 16 '22 08:10

Yang Shen - MSFT


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')
like image 1
Joey Eng Avatar answered Oct 16 '22 10:10

Joey Eng