I created a variable called Test
in Pipeline UI and left the value as blank. So it appeared as shown below.
I'm trying to access this variable in YAML pipeline but unable to check it's empty value.
I'm trying like this :
- variables
- name: 'Release'
${{ if or(eq(variables['Test'],''), eq(variables['Test'], 'undefined')) }}:
value: 'Stage'
${{ if and(ne(variables['Test'],''), ne(variables['Test'], 'undefined')) }}:
value: 'Test'
The variable Release
value is always Stage
whether Test
variable is empty or any value in it.
I think there must be an easy way to check whether a variable value is empty or not?
The correct check would be to use not(...)
to check if the value is defined. However, it seems you can't use these variables for compile time expressions, maybe because the template is already compiled at this point. Only built-in variables seem to work for expressions.
At least, this always returns empty for me:
variables:
- name: CopyVariable
value: ${{variables['testvariable']}}
You could use Parameters instead. Though It would probably be best to use a non-empty string here as a default and restrict the possible values.
parameters:
- name: testparam
type: string
default: ''
variables:
# Always empty
- name: CopyVariable
value: ${{variables['testvariable']}}
# Works fine
- name: CopyVariable2
value: ${{variables['Build.SourceBranch']}}
- name: ByParameter
${{ if not(parameters['testparam']) }}:
value: 'no param'
${{ if not(not(parameters['testparam'])) }}:
value: 'has param'
- name: ByVariable
# first one is always empty, second one works.
value: 'Compile time: ${{variables.testvariable}} -- Runtime: $(testvariable)'
steps:
- script: |
echo Hello, $(CopyVariable) - $(testvariable)
displayName: 'Copy Variable'
- script: |
echo Hello, $(CopyVariable2)
displayName: 'Copy Variable2'
- script: |
echo Hello, $(ByParameter)
displayName: 'By Parameter'
- script: |
echo Hello, $(ByVariable)
displayName: 'By Variable'