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.