Reputation: 560
I have a pipeline with multiple stages, and each stage has a common option. I want to control that option for each stage individually, but also have a way to force it off for all stages. The YAML looks roughly like this:
parameters:
-name: globalFlag
type: boolean
default: true
-name: stageSettings
type: object
default:
- stageName: stageOne
stageFlag: true
- stageName: stageTwo
stageFlag: false
...
stages
- ${{each stageDef in parameters.stageSettings }}:
...
myFlag: ${{ and(parameters.globalFlag, stageDef.stageFlag) }}
That almost works, except I believe stageDef.stageFlag
is being interpreted as a string as the last line always evaluates to true
if parameters.globalFlag
is true
. Which is odd as if that line is just myFlag: ${{ stageDef.stageFlag }}
it works as expected.
I can work around this by changing the line to
myFlag: ${{ and(parameters.globalFlag, eq(stageDef.stageFlag, 'true')) }}
This is pretty good and even works for True
and TRUE
as well as true
. But it doesn't work for Yes
or On
which are also true
in YAML. So it could easily trip up someone when they enter a true value and it doesn't work as expected.
How can I get the property stageFlag
in my object type to be interpreted as a boolean? Is there a way to mark the type of a property within an object? Is there a way to cast a string to a boolean that handles all valid ways of entering a boolean value?
Upvotes: 2
Views: 3147
Reputation: 1646
This could be due to the fact that all YAML variables are stored as string in an Azure DevOps pipeline.
Your parameters on the other hand can use different data types.
Despite that globalFlag
is a boolean, the stagesettings
is a YAML object so the all YAML variables are stored as a string unfortunately applies here.
But as you discovered, using expressions like 'eq' can solve your issue a bit through type casting the string in the expression.
I don't think there is another way with this object parameter, nor is there a way to define types to the variables.
Upvotes: 2