The following azure pipeline code gives error
'A template expression is not allowed in this context'
variables:
major: 2020
minor: 3
patch: 1
major_minor_patch: $(major).$(minor).$(patch)
trigger:
- master
- Dev
- release/R${{variables.major_minor_patch}}
- release/${{variables.major_minor_patch}}/*
My intention is to use the major, minor and patch variables to specify the branches that would form the CI trigger, instead of hard coding it in the pipeline YAML.
Thanks
Variables in trigger block are not supported. See document here for more information.
Trigger blocks can't contain variables or template expressions.
If you do not want the pipeline be triggered by other branches, you can try the workaround below.
Create an additional pipeline to check if the source branch matches release/major_minor_patch. Then trigger the main pipeline in this additional pipeline.
variables:
major: 2020
minor: 3
patch: 1
triggerMain: false
trigger:
branches:
include:
- releases/*
steps:
- powershell: |
$branch = "$(Build.SourceBranchName)"
if ($branch -match "$(major).$(minor).$(patch)") {
echo "##vso[task.setvariable variable=triggerMain]True" #set variable triggerMain to true if matches.
}
- task: TriggerBuild@3
inputs:
definitionIsInCurrentTeamProject: true
buildDefinition: '56' #{id of your main pipeline}
queueBuildForUserThatTriggeredBuild: true
ignoreSslCertificateErrors: false
useSameSourceVersion: true
useSameBranch: true
waitForQueuedBuildsToFinish: false
storeInEnvironmentVariable: false
authenticationMethod: 'Personal Access Token'
password: '$(system.accesstoken)'
enableBuildInQueueCondition: false
dependentOnSuccessfulBuildCondition: false
dependentOnFailedBuildCondition: false
checkbuildsoncurrentbranch: false
failTaskIfConditionsAreNotFulfilled: false
condition: eq(variables['triggerMain'], 'True')
In the above pipeline, it will be triggered first to check if the source branch match the format. If it matches, then task TriggerBuild will be executed to trigger the main pipeline.