Azure管道参数参考与动态键

huangapple go评论63阅读模式
英文:

Azure pipeline parameter reference with dynamic keys

问题

在Azure管道的.yml文件中,我有以下行:

variables:
 - group: WebAppDevelopment_2023

这是有效的。

为了防止重复,我有一个循环:

- ${{ each stage in parameters.environmentsGates }}:
  - stage:
    displayName: 将开发构建发布到开发环境
    variables:
    - group: WebAppDevelopment_2023

还有:

 parameters:
  - name: 'variableGroups'
    type: object
    default:
      - Development: 'WebbDevelopment_2023'
      - UAT: 'UAT_2032'
      - Production: 'ProdEnv'
  - name: 'environmentsGates'
    type: object
    default: 
      - environmentName: Development
        # 其他键/值
      - environmentName: UAT
        # 其他键/值
      - environmentName: Production
        # 其他键/值

我的问题是:

在循环的每次迭代中,我有一个不同的值stage.environmentName,这应该是parameters.variableGroups的键。以下是示例:

variables:
    - group: ${{parameters.variableGroups[${{stage.environmentName}}].value}}

如何使其正常工作,我尝试了许多宏、运行时等语法组合,但都没有成功。

我尝试了条件语句,同样没有成功,例如:

variables:
    - ${{each group in parameters.variableGroups}}:
        - ${{ if eq(group.Value,  '${{stage.environmentName}}' )}}:
              - group: ${{group.Value}}
          ${{ else }}:
              - group: WebAppDevelopment_2023
    - name: token
      value: $(azure-deploy-token)

返回错误:

在尝试读取映射结束时出现意外状态。状态:

MappingState:
  IsStart:True
  Index:0
  IsKey:False
  IsEnd:False

SequenceState:
  IsStart:False
  Index:0
  IsEnd:False

EachExpressionState:
  IsSequenceInsertion:True
  IsStart:False
  Index:1
  IsEnd:False

MappingState:
  IsStart:False
  Index:0
  IsKey:True
  IsEnd:False

SequenceState:
  IsStart:False
  Index:0
  IsEnd:[...]

希望这能帮助您解决问题。

英文:

Within an Azure pipeline .yml file I have this line:

variables:
 - group: WebAppDevelopment_2023

This works fine.

To prevent repetition i have an loop:

- ${{ each stage in parameters.environmentsGates }}:
  - stage:
    displayName: Publish DEVELOPMENT build to development environment
    variables:
    - group: WebAppDevelopment_2023

There is also:

 parameters:
  - name: 'variableGroups'
    type: object
    default:
      - Development: 'WebbDevelopment_2023'
      - UAT: 'UAT_2032'
      - Production: 'ProdEnv'
  - name: 'environmentsGates'
    type: object
    default: 
      - environmentName: Development
        # other key/values
      - environmentName: UAT
        # other key/values
      - environmentName: Production
        # other key/values

My question is:

On each iteration of the loop i have a different value for stage.environmentName and that should be the key for parameters.variableGroups. Example below

variables:
    - group: ${{parameters.variableGroups[${{stage.environmentName}}].value}}

How do I get this working, I have tried many combinations macro, runtime, etc syntax but no success

I've experimented with conditionals, again without success: E.g.


    variables:
        - ${{each group in parameters.variableGroups}}:
            - ${{ if eq(group.Value,  '${{stage.environmentName}}')}}:
                  - group: ${{group.Value}}
              ${{ else }}:
                  - group: WebAppDevelopment_2023
        - name: token
          value: $(azure-deploy-token)

Returns error:

Unexpected state while attempting to read the mapping end. State:

MappingState:
  IsStart: True
  Index: 0
  IsKey: False
  IsEnd: False

SequenceState:
  IsStart: False
  Index: 0
  IsEnd: False

EachExpressionState:
  IsSequenceInsertion: True
  IsStart: False
  Index: 1
  IsEnd: False

MappingState:
  IsStart: False
  Index: 0
  IsKey: True
  IsEnd: False

SequenceState:
  IsStart: False
  Index: 0
  IsEnd:[...]

答案1

得分: 2

I simplified your case but it works here in this way

parameters:
  - name: 'variableGroups'
    type: object
    default:
      - key: Development
        name: 'WebbDevelopment_2023'
      - key: UAT
        name: 'UAT_2032'
      - key: Production
        name: 'ProdEnv'
  - name: 'environmentsGates'
    type: object
    default:
      - environmentName: Development
      - environmentName: UAT
      - environmentName: Production

trigger:
- grog
- master

pool:
  vmImage: ubuntu-latest

steps:
- ${{ each stage in parameters.environmentsGates }}:
  - ${{ each varg in parameters.variableGroups }}:
      ${{ if eq(varg.key, stage.environmentName) }}:
        script: |
            echo '${{ stage.environmentName }}'
            echo '${{ varg.name }}'            

Azure管道参数参考与动态键

英文:

I simplified your case but it works here in this way

parameters:
  - name: 'variableGroups'
    type: object
    default:
      - key : Development
        name : 'WebbDevelopment_2023'
      - key : UAT
        name: 'UAT_2032'
      - key: Production
        name : 'ProdEnv'
  - name: 'environmentsGates'
    type: object
    default: 
      - environmentName: Development
      - environmentName: UAT
      - environmentName: Production

trigger:
- grog
- master

pool:
  vmImage: ubuntu-latest

steps: 
- ${{ each stage in parameters.environmentsGates }}:
  - ${{ each varg in parameters.variableGroups }}:
      ${{ if eq(varg.key, stage.environmentName) }}: 
        script: |
            echo '${{ stage.environmentName }}'
            echo '${{ varg.name }}'

Azure管道参数参考与动态键

huangapple
  • 本文由 发表于 2023年6月29日 20:04:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580879.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定