Search code examples
azure-devopstypesyamlazure-pipelines-yamlazure-devops-extensions

How to declare or use a boolean variable n YAML?


I am using a task template in Azure Devops which is like the snippet below

      - task: sampletask@0
        inputs:
          flag1: true
          flag2: true

The flag1&2 are expected to be boolean values there. But instead of making it hardcoded as 'true', there is an option to pass a string as an external variable to set the value.

But when I try to declare externalVar1&2 externally as 'true', and try:

      - task: sampletask@0
        inputs:
          flag1: $[$(externalVar1), 'true')]  --- Incorrect type. Expected "boolean".
          flag2: $[$(externalVar2), 'true')]  --- Incorrect type. Expected "boolean".

So is there a feasible way to evaluate an external string expression, say $(expr), into a boolean variable/object and pass into the target flag parameters that expect boolean type?


Solution

  • The variables in the concept of Azure DevOps pipeline are always string type, no other type including boolean. You can only handle the boolean type in your script(You can convert the string to boolean in your script and handle them there.).

    If you are about to evaluate the variable value for afterward operation or tasks and jobs, you could refer to the custom conditions.

    and(succeeded(), eq(variables['Flag1'], 'value'))