I have three level nested Azure DevOps yaml templates. In my second level nested template I want to conditionally call the third level template based on the target branch for pull request.
For e.g. level3@templates should only be called if pull request target branch is Main.
I know System.PullRequestTargetBranch is not available in templates. I am also familiar with
and limitations pre-defined variables have with nested templates
I am hoping someone will be able to suggest some workaround for getting around this problem.
Will appreciate all the help.
My yaml is structured as below
resources:
repositories:
- repository: templates
type: git
name: MyProj/templates
ref: refs/heads/main
extends:
template: extends/level1.yaml@templates
parameters:
MY_STAGES:
BUILD:
displayName: "Build"
L1Config:
config1: "config1"
parameters:
- name: MY_STAGES
type: object
default: {}
- name: L1Config
type: object
default: {}
variables:
- name: var1
value: "value1"
- name: var2
value: "val2"
stages:
- ${{ each MY_STAGE in parameters.MY_STAGES }}:
- stage: ${{ MY_STAGE.key }}
displayName: "Build"
jobs:
- job: build_dev
displayName: Build
steps:
- template: /jobs/level2@templates
parameters:
L2Config: ${{ MY_STAGE.value.L1Config }}
parameters:
- name: L2Config
type: object
default: {}
steps:
- pwsh: Write-Host "Hello World!"
# This is the template I want to call conditionally, based on the PullRequest Target
- ${{ if eq(variable['System.PullRequest.TargetBranch'], 'refs/heads/main') }}:
# I know above syntax does not work, I have just written it down as an example of what I wish had worked.
- template: /steps/level3@templates
parameters:
param1: value1
The System.PullRequestBranch information can be used in Task Conditions. So in effort to keep things simple, I have moved the check for PR Target branch to the task condition of task which needed this information.