Ola Eldøy
Ola Eldøy

Reputation: 5936

How to fail a pipeline gracefully when a variable is not defined

I am using a pipeline variable to define a path for a deploy script. There is the danger that someone forgets to define the variable. What would be a good way to detect this and give the appropriate error message in the yaml script file?

I could create a PowerShell script that would fail if the variable is not defined. But I would prefer to keep it all in the yaml file.

Upvotes: 5

Views: 1870

Answers (1)

Yan Sklyarenko
Yan Sklyarenko

Reputation: 32270

The PowerShell script to examine the variable value can be tiny and can still live in the YAML as the inline script of the PowerShell task:

- powershell:  if (!$env:MyVar) Write-Error "The variable is not set"
  displayName: Check Prerequisite Variable
  failOnStderr: true
  errorActionPreference: stop

I might be mistaken on syntax, but it describes the idea.

Upvotes: 6

Related Questions