Access Pipeline Queue variable in YAML pipeline

1.3k Views Asked by At

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?

3

There are 3 best solutions below

1
Alex AIT On BEST ANSWER

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'
0
Krzysztof Madej On

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)'
2
Kevin Lu-MSFT On

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'