英文:
Problem referencing predefined variable in {{ if/elseif/else }} template syntax in Azure Pipelines YAML
问题
以下是您要翻译的内容:
在以下编辑和简化的 Azure Pipelines YAML 片段中,我无法将 distributionGroupIds 设置为 <guid2>
。
如下所示,它返回 <guid1>
。
如果我将 'merge' 更改为 'whatever',它返回 <guid3>
在这两种情况下,发布说明显示为:
[merge]
[refs/head/my-branch-name]
所以,为什么 elseif 不会评估为真,distributionGroupIds 不会设置为 <guid2>
?
重新阅读我的问题,它确实存在 XY 问题。从根本上说,我的目标是将特定 PR 源分支构建分发到特定的分发组。上述方法不起作用。我该如何实现我的目标?
英文:
In the following edited and simplified snippet of Azure Pipelines YAML, I'm not able to get distributionGroupIds set to <guid2>
.
As shown below, it returns <guid1>
.
If I change 'merge' to 'whatever' if returns <guid3>
In both cases, the release notes show as:
[merge]
[refs/head/my-branch-name]
So, why doesn't the elseif evaluate to true and distributionGroupIds get set to <guid2>
?
- template: publish-appcenter.yml
parameters:
${{ if eq(variables['Build.SourceBranchName'], 'merge') }}:
distributionGroupIds: '<guid1>'
${{ elseif eq(variables['system.pullRequest.sourceBranch'], 'refs/heads/my-branch-name') }}:
distributionGroupIds: '<guid2>'
${{ else }}:
distributionGroupIds: '<guid3>'
releaseNotesText: |
[$(Build.SourceBranchName)] #prints [merge]
[$(system.pullRequest.sourceBranch)] #prints [refs/heads/my-branch-name]
Rereading my question it does have an XY Problem. Fundamentally my objective is to distribute specific PR source branch builds to specific distribution groups. The above approach isn’t working. How can I achieve my objective?
答案1
得分: 2
After more (a lot more!) trial and error, I think I understand now that the ${{ if }} is evaluated at ‘compile time’.
And for whatever reason, variables['Build.SourceBranchName'] is available at compile time but variables['system.PullRequest.SourceBranch'] is not.
Very few of the predefined variables seem to be defined and usable at compile time. The only one that gets me close to where I want to be is Build.SourceBranch, which evaluates to something like refs/head/734/merge. Where 734 is the PR number. So, I can use:
${{ if contains(variables['Build.SourceBranch'], 734) }}
I’d prefer to use branch name, but PR number is an acceptable proxy.
英文:
After more (a lot more!) trial and error I think I understand now that the ${{ if }} is evaluated at ‘compile time’.
And for whatever reason variables[‘Build.SourceBranchName’] is available at compile time but variables[‘system.PullRequest.SourceBranch’] is not.
Very few of the predefined variables seem to be defined and usable at compile time. The only one that gets me close to where I want to be is Build.SourceBranch which evaluates to something like refs/head/734/merge. Where 734 is the PR number. So, I can use:
${{ if contains(variables[‘Build.SourceBranch’], 734) }}
I’d prefer to use branch name but PR number is an acceptable proxy.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论