Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use boolean variable as lowercase string in Azure Devops YML pipeline script

I have the following variable in my pipeline script:

variables:
  is_main: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')]

And I want to use it like this:

- script: npm run build -- --source-map=$(is_main)

Unfortunately that leads to an error because "True" is not allowed:

> ng build "--source-map=True"

Cannot parse arguments. See below for the reasons.
    Argument --source-map could not be parsed using value "True".Valid type(s) is: boolean
##[error]Bash exited with code '1'.
Finishing: Angular build

I need to lowercase the boolean, and I want to do so in the script step to keep the variable in the pipeline as a true boolean (as opposed to a string) until the last possible moment. I've tried several options. To summarize:

Option 1 $(is_main) leads to:

Argument --source-map could not be parsed using value "True".Valid type(s) is: boolean

Option 2 $[lower($(is_main))] leads to:

lower(True): syntax error in expression (error token is "(True)")

Option 3 $[lower(variables('is_main'))] leads to:

 lower(variables('is_main')): syntax error in expression (error token is "(variables('is_main'))")

Option 4 $[lower(variables['is_main'])] leads to:

 lower(variables['is_main']): syntax error in expression (error token is "(variables['is_main'])")

Option 5 ${{ lower(variables.is_main) }} (which should not work as far as I could tell from documenation, because it is not used for runtime evaluation, but hey....) leads to:

eq(variables['build.sourcebranch'], 'refs/heads/main'): syntax error in expression (error token is "(variables['build.sourcebranch'], 'refs/heads/main')")

I've read through the "Expressions" documentation on Azure Pipelines MSDN pages and my above attempts are based on it already.

What is the proper way to convert a boolean variable in an Azure Devops pipeline to a lowercase string to be used inside a script step?


Footnote:

For now I use this workaround:

variables:
  is_main: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')]
  generate_source_maps: $[lower(variables['is_main'])] # script step requires lowercase boolean

And use it like this:

- script: npm run build -- --source-map=$(generate_source_maps)

Quite suboptimal because generate_source_maps is a string whereas the name suggests it's a boolean, and the conversion to lowercase happens very far from the place in the yml where it is relevant and obvious that this is needed. But it works.

like image 687
Jeroen Avatar asked Jan 20 '26 17:01

Jeroen


1 Answers

My team and I encountered a similar problem just now [A], and convertToJson worked for us as a way to convert a Pipeline boolean value to lowercase string literal.

Since your initial value is a non-static variable, I don't think you can inline this expression into your script line though [B] - you probably still need an extra variable.

Perhaps this would work for you?

variables:
  is_main: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')]
  is_main_str: $[convertToJson(variables['is_main'])]

- script: npm run build -- --source-map=$(is_main_str)

[A]: Specifically, we wanted to pass a Pipeline parameter value of type boolean to an ARM template parameter via overrideParameters on an AzureResourceManagerTemplateDeployment@3 task.

[B]: According to the docs, Compile time expressions can be used anywhere; runtime expressions can be used in variables and conditions., and only runtime expressions can access non-static variables. Looks like the $() macro syntax cannot evaluate an expression.

like image 65
pmalmsten Avatar answered Jan 22 '26 11:01

pmalmsten