Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Accessing variable groups in conditions

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Update:

We can also use parameters in pipeline to set the value:

For example:

trigger:
  batch: true
parameters:
- name: dev
  type: string
  default: refs/heads/branch-1 
- name: test
  type: string
  default: refs/heads/branch-1 
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'], parameters.dev) }}:
          - dev
      - ${{ if startsWith(variables['Build.SourceBranch'], parameters.test) }}:
          - test
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading