Problem referencing predefined variable in {{ if/elseif/else }} template syntax in Azure Pipelines YAML

huangapple go评论63阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月6日 03:38:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409534.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定