【问题标题】:Access Pipeline Queue variable in YAML pipeline在 YAML 管道中访问管道队列变量
【发布时间】:2020-11-07 16:57:11
【问题描述】:

我在 Pipeline UI 中创建了一个名为 Test 的变量,并将值留空。于是出现如下图。

我正在尝试在 YAML 管道中访问此变量,但无法检查它是否为空值。

我正在尝试这样:

- 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'

变量Release 的值始终为Stage,无论Test 变量为空还是其中的任何值。

我认为必须有一种简单的方法来检查变量值是否为空?

【问题讨论】:

  • 嗨@Venky。机票有更新吗?如果答案能给你一些帮助,你可以考虑accepting the useful one as answer。这样它可以帮助遇到同样问题的其他社区成员,谢谢。

标签: azure azure-devops yaml devops multistage-pipeline


【解决方案1】:

正确的检查是使用not(...) 来检查值是否已定义。但是,您似乎不能将这些变量用于编译时表达式,可能是因为此时模板已经编译。似乎只有内置变量适用于表达式。

至少,这对我来说总是空的:

variables:
  - name: CopyVariable
    value: ${{variables['testvariable']}}

您可以改用Parameters。虽然最好在此处使用非空字符串作为默认值并限制可能的值。

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'

【讨论】:

  • 运行时参数似乎是我的问题的干净解决方案,它按预期工作。 variables 有很多限制,不适合大多数用例。
【解决方案2】:

如果您想检查变量是否为空,您不能在 YAML 中执行此操作。但是您可以在 Powershell 中执行此操作并记录命令以设置变量:

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)'

【讨论】:

    【解决方案3】:

    我想分享另一种方法来检查变量值是否为空。

    根据我的测试,条件可以得到队列变量。

    您可以使用Condition. 在管道作业中检查变量是否为空

    这里是 Yaml 示例:

    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 -> 发布:Stage

    非空 -> 发布:测试

    更新:

    如果还想在if表达式中使用队列变量,可以在yaml中定义一个变量,用它来获取队列变量的值进行判断。

    示例如下:

     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'
     
    
    
     
    

    【讨论】:

    • 所以队列变量只能在condition中访问,而不能在其他地方访问?
    • 嗨@Venky。 If expression 似乎无法直接获取队列变量。我还找到了另一种解决方法,请检查更新。
    猜你喜欢
    • 1970-01-01
    • 2021-08-31
    • 2019-01-27
    • 2022-07-21
    • 1970-01-01
    • 2019-10-04
    • 2021-05-12
    • 2019-11-17
    • 1970-01-01
    相关资源
    最近更新 更多