Accessing variable groups in conditions

  Kiến thức lập trình

I am having the following pipeline definition

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

My variables are defined in the Azures Libraly as:

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

The problem is the eq is considered always false, even if the build branch is refs/heads/branch-1, and the startsWith is always true. I am suspecting that the values are always empty

Based on your YAML sample, the variables are defined in the variable group. The variables in variable group will expand at runtime, but the if expression will expand at compile time.

In this case, the variable: variables['dev'] is empty at the compile time.

To solve this issue, you can directly define the variables in the YAML Variables field.

For example:

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

LEAVE A COMMENT