代码之家  ›  专栏  ›  技术社区  ›  Vyas Bharghava

如何:动态解析azuredevops YAML中的嵌套变量

  •  0
  • Vyas Bharghava  · 技术社区  · 5 年前

    azuredevops YAML中变量支持的一致性和语法差异很大。 例如:

    trigger:
    - master
    
    # Variable Group has $(testCategory1) with value
    # 'TestCategory=bvttestonly | TestCategory=logintest'
    variables:
      - group: DYNAMIC_VG
    
    jobs:
      - job:
        pool: 'MyPool' #Has about 10+ self hosted agents
    
        strategy:
          parallel: $[ variables['noOfVMsDynamic']]
    
        variables:
          indyx: '$(testCategories$(System.JobPositionInPhase))'
          indyx2: $[ variables['indyx'] ] 
          testCategories: $[ variables[ 'indyx2' ] ]
    
        steps:
        - script: |
            echo "indyx2 - $(indyx2)"
            echo "testCategories $(testCategories)"
          displayName: 'Display Test Categories'
    

    步骤打印:

    "indyx2 - $(testCategories1)"
    "testCategories $(testCategories1)"
    

    我需要打印变量组中定义的$(testCategories1)的值:

    'TestCategory=bvttestonly | TestCategory=logintest'

    0 回复  |  直到 5 年前
        1
  •  2
  •   Leo Liu    5 年前

    如何:动态解析azuredevops YAML中的嵌套变量

    因为嵌套变量的值 $(testCategories$(System.JobPositionInPhase)) )目前在构建管道中还不支持。

    这就是为什么你总是得到价值 $(testCategories1) 而不是变量的实际值 testCategories1 .

    我在工作中多次遇到这个问题 past posts 在azuredevops支持此功能之前,我们还没有一个完美的解决方案。

    为了方便测试,我简化了您的yaml,如下所示:

    jobs:
      - job: ExecCRJob
        timeoutInMinutes: 800
    
        pool:
          name: MyPrivateAgent
    
        displayName: 'Execute CR'
    
    
        variables:
          testCategories1: 123456
          testCategoriesSubscripted: $(testCategories$(System.JobPositionInPhase))
    
        strategy:
          parallel: $[variables['noOfVMs']]     
        steps:
        - template: execute-cr.yml
          parameters:
            testCategories: $(testCategoriesSubscripted)
    

    这个 execute-cr.yml :

    steps:
        - script: echo ${{ parameters.testCategories }}
    

    我们总是得到 $(测试类别1) 不是它的价值。

    如果 我换了衣服 $(测试类别)$(系统工作位置同相)) $(测试类别1) ,一切正常。

    因为嵌套变量还不受支持, 作为解决方法 ,我们需要为的每个值展开嵌套变量 testCategories ,例如:

    - job: B
      condition: and(succeeded(), eq(dependencies.A.outputs['printvar.skipsubsequent'], 'Value1'))
      dependsOn: A
      steps:
      - script: echo hello from B
    

    检查 Expressions Dependencies 更多细节。

    希望这有帮助。

        2
  •  0
  •   Bevan    5 年前

    如果我正确理解了您的问题,那么问题是管道在作业运行时计算所有变量。此场景中的解决方案是将任务拆分为具有依赖关系的单独作业。

    看看我在这篇文章中的答案,如果你想要的是: YAML pipeline - Set variable and use in expression for template

        3
  •  0
  •   Luizgrs    5 年前

    这可能对您有用:

    variables
       indyx: $[ variables[format('{0}{1}', 'testCategories', variables['System.JobPositionInPhase'])] ]
    

    在一个稍微不同的情况下,它对我有效,这也需要一些动态变量名。