0

I created a variable called Test in Pipeline UI and left the value as blank. So it appeared as shown below.

enter image description here

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?

Venkata Dorisala
  • 4,783
  • 7
  • 49
  • 90
  • Hi @Venky. Is there any update about ticket? If the answers could give you some help, you may consider [accepting the useful one as answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work).so it could help other community members who get the same issues ,thanks. – Kevin Lu-MSFT Jul 22 '20 at 09:43

3 Answers3

0

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'
Alex AIT
  • 17,361
  • 3
  • 36
  • 73
  • 1
    runtime parameters seems to be clean solution for my issue here and it worked as expected. `variables` have so many limitations and not suitable for majority of use cases. – Venkata Dorisala Jul 22 '20 at 10:19
0

If you want to need to check if variable is empty you can't do this in YAML. But you can do this in Powershell and logging commands to set variable:

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

jobs:
- job: Test
  pool:
    vmImage: 'ubuntu-latest'
  variables:
    varOS: $(Agent.OS)
  steps:
    - pwsh: |
        $testVar = ''
        $test = '$(Test)'
        if ($test -eq 'undefined' -or $test -eq '') {
          $testVar = "Stage";
        } elseif ($test -ne 'undefined' -and $test -ne '') {
          $testVar = "Test";
        }
        Write-Host $testVar
        Write-Host "##vso[task.setvariable variable=Release;isOutput=true]$testVar"
      name: Initialize
    - pwsh: |
        echo '$(Initialize.Release)'
Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
0

I would like to share another method to check if a variable value is empty or not.

Based on my test, the condition could get Queue Variable.

You could check if the variable is empty in Pipeline Job with Condition.

Here is the Yaml sample:

steps:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script:  Write-Host "##vso[task.setvariable variable=Release;]Stage"
  condition: or(eq(variables['Test'],''), eq(variables['Test'], 'undefined'))

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: Write-Host "##vso[task.setvariable variable=Release;]Test"
  condition:  and(ne(variables['Test'],''), ne(variables['Test'], 'undefined'))

- script: echo $(Release)

' ' or Empty -> Release: Stage

Not empty -> Release: Test

Update:

If you still want to use queue variable in the if expression, you can define a variable in yaml and use it to get the value of the queue variable for judgment.

Here is the example:

 variables: 
   - name: aaa
     value: $[variables.test]
   - name: Release
     ${{  if or(eq(variables['aaa'],''), eq(variables['aaa'], 'undefined')) }}:
      value: 'Stage'
     ${{  if and(ne(variables['aaa'],''), ne(variables['aaa'], 'undefined')) }}:
      value: 'Test'
 


 
Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28