访问条件中的变量组

问题描述 投票:0回答:1

我有以下管道定义

trigger:
  batch: true

resources:
  repositories:
  - repository: templates
    type: git
    ref: refs/heads/master
    name: my-template

variables:
- group: deployment-branches

extends:
  template: my-template.yml@templates
  parameters:
    ${{ if ne(variables['Build.Reason'], 'PullRequest') }}:
      deploy_stages:
      - ${{ if eq(variables['Build.SourceBranch'], variables['dev']) }}:
          - dev
      - ${{ if startsWith(variables['Build.SourceBranch'], variables['test']) }}:
          - test

我的变量在 Azures Libraly 中定义为:

dev: refs/heads/branch-1
test: refs/heads/branch-1

问题是

eq
始终被认为是
false
,即使构建分支是
refs/heads/branch-1
,并且
startsWith
始终是
true
。我怀疑这些值始终为空
 

azure-pipelines
1个回答
0
投票

根据您的 YAML 示例,变量在变量组中定义。变量组中的变量会在运行时扩展,但 if 表达式会在编译时扩展。

在这种情况下,变量:

variables['dev']
在编译时为空。

要解决此问题,您可以直接在 YAML Variables 字段中定义变量。

例如:

trigger:
  batch: true

resources:
  repositories:
  - repository: templates
    type: git
    ref: refs/heads/master
    name: my-template

variables:
- name: dev
  value: refs/heads/branch-1
- name: test
  value: refs/heads/branch-1
 
extends:
  template: my-template.yml@templates
  parameters:
    ${{ if ne(variables['Build.Reason'], 'PullRequest') }}:
      deploy_stages:
      - ${{ if eq(variables['Build.SourceBranch'], variables['dev']) }}:
          - dev
      - ${{ if startsWith(variables['Build.SourceBranch'], variables['test']) }}:
          - test
© www.soinside.com 2019 - 2024. All rights reserved.