This is part of my azure pipeline yaml
pool:
vmImage: ubuntu-latest # Use 'windows-latest' if you have Windows native +Node modules
variables:
${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
brname: $[variables['System.PullRequest.targetBranchName']]
isPreRelease: $[eq(variables['brname'], 'main')]
${{ if or(eq(variables['isPreRelease'], 'True'), eq(variables['isPreRelease'], True)) }}: # ---- not working
releasename: $[ replace(variables['System.PullRequest.SourceBranch'], 'refs/heads/release/', '') ]
format: "v{0}-rc.{1}"
patch_version: $[counter(variables['releasename'], 1)]
${{ else }}:
releasename: $[ variables['System.PullRequest.PullRequestId'] ]
format: "PR-{0}-{1}"
patch_version: $[counter(variables['releasename'], 1)]
buildname: $[format(variables['format'],variables['releasename'],variables['patch_version'])]
${{ else }}:
format: "development-{0}"
buildname: $[ format(variables['format'],counter('development', 1)) ]
Although those intermediate variables brname and isPreRelease are not really required, i added them to debug the problem. here is the output from the variable calculation
The job parameters in pipeline look like this
Job preparation parameters
Variables:
brname:
Parsing expression: <variables['System.PullRequest.targetBranchName']>
Evaluating: variables['System.PullRequest.targetBranchName']
Result: 'main'
isPreRelease:
Parsing expression: <eq(variables['brname'], 'main')>
Evaluating: eq(variables['brname'], 'main')
Expanded: eq('main', 'main')
Result: 'True'
releasename:
Parsing expression: <variables['System.PullRequest.PullRequestId']>
Evaluating: variables['System.PullRequest.PullRequestId']
Result: '473'
patch_version:
Parsing expression: <counter(variables['releasename'], 1)>
Evaluating: counter(variables['releasename'], 1)
Expanded: 16
Result: '16'
buildname:
Parsing expression: <format(variables['format'],variables['releasename'],variables['patch_version'])>
Evaluating: format(variables['format'], variables['releasename'], variables['patch_version'])
Result: 'PR-473-16'
7 queue time variables used
system.pullRequest.pullRequestId : 473
system.pullRequest.sourceBranch : refs/heads/release/2.1.0
system.pullRequest.targetBranch : refs/heads/main
system.pullRequest.targetBranchName : main
system.pullRequest.sourceCommitId : 421e608d30116fc89a8b45ccccdcd39d982074a7
system.pullRequest.sourceRepositoryUri : https://[email protected]/CareAbout/DataIntegration/_git/data-factory
system.pullRequest.pullRequestIteration : 30
Why is the second if statement not being calculated correctly?
2
Answers
You’re mixing runtime expressions (
$[]
) with compilation-time expressions (${{}}
).isPreRelease
isn’t defined until after the YAML template is compiled. Compilation-time expressions are evaluated very early in the YAML pipeline, before runtime expressions are evaluated.I tested the issue and found that it will not work when use
${{ if eq(variables['System.PullRequest.targetBranchName'], 'main') }}:
.The
System.PullRequest.targetBranchName
is not available in compile time.As a workaround, I can use
System.PullRequest.targetBranchName
in the conditions session. The following sample is using theSystem.PullRequest.targetBranchName
in job conditions and the right job will run.Result: