Home > Software design >  Jenkins (Pipeline) Boolean Parameter in Powershell confusing
Jenkins (Pipeline) Boolean Parameter in Powershell confusing

Time:04-18

I have a parametrized Jenkins Pipeline Script, where I pass a Boolean Parameter "isModuleUpdate".

When I use this parameter in my Pipeline Script I get confusing results. My Script:

 Write-Host ">>> isModuleUpdate as String: $Env:isModuleUpdate"
 Write-Host ">>> isModuleUpdate as Variable: " $Env:isModuleUpdate
 if ($Env:isModuleUpdate) {
      Write-Host ">>> ModuleUpdate is checked!"
 }

When I run my Script, the Result is:

>>> isModuleUpdate as String: false
>>> isModuleUpdate as Variable: false
>>> ModuleUpdate is checked!

What is the sexiest way to check this variable corectly?

CodePudding user response:

I recall having issues with checking booleans in PowerShell as well. Ultimately, -eq $true worked:

 if ($Env:isModuleUpdate -eq $true) {

CodePudding user response:

From about_Environment_Variables:

Environment variables, unlike other types of variables in PowerShell, are always stored as a string and can't be empty.

Your if statement evaluates to true, because your string variable is not empty. In other words, it contains the string false and not a boolean value. Do a proper string comparison instead:

if ($Env:isModuleUpdate -like 'true') {...
  • Related