cheslijones
cheslijones

Reputation: 9194

Conditional in pipeline always triggering "Object reference not set to an instance of an object."

I have the following template which should be setup such that it always passes given that I'm just testing this out:

parameters:
- name: services
  type: object
  default:
  - admin-v2

stages:
- stage: UnitTests
  displayName: Run unit tests on service...
  dependsOn: Changed
  condition: succeeded()
  jobs:
  - job: UnitTests
    condition: or(eq(stageDependencies.Changed.Changes.outputs['detectChanges.anyServicesChanged'], true), eq(variables['Build.Reason'], 'Manual'))
    displayName: Running unit tests...
    steps:
    - ${{ each service in parameters.services }}:
      ${{ if eq(${{ service }}, 'admin-v2') }}:
      - bash: |
          echo "Now running ${{ service }} unit tests..."

The following line is causing and error:

...
${{ if eq(${{ service }}, 'admin-v2') }}:
...

There is:

An error occurred while loading the YAML build pipeline. Object reference not set to an instance of an object.

I'm pretty much using a watered-down version of this example.

I'm not sure why it fails because it definitely looks to me like it should pass.

Any suggestions for how to fix this or why this is causing an error?

Upvotes: 0

Views: 667

Answers (1)

cheslijones
cheslijones

Reputation: 9194

Finally figured it out...

Turns out the ${{ }} is not necessary in the if, which is an oversight on my part.

So it should just be:

parameters:
- name: services
  type: object
  default:
  - admin-v2

stages:
- stage: UnitTests
  displayName: Run unit tests on service...
  dependsOn: Changed
  condition: succeeded()
  jobs:
  - job: UnitTests
    condition: or(eq(stageDependencies.Changed.Changes.outputs['detectChanges.anyServicesChanged'], true), eq(variables['Build.Reason'], 'Manual'))
    displayName: Running unit tests...
    steps:
    - ${{ each service in parameters.services }}:
      - ${{ if eq(service, 'admin-v2') }}:
        - bash: |
            echo "Now running ${{ service }} unit tests..."

Upvotes: 1

Related Questions