Azure Pipelines - Build.SourceVersionMessage 变量在表达式函数中不起作用

JSO*_*ulo 6 azure-pipelines azure-pipelines-yaml

我正在尝试基于运行 Azure DevOps Server 2020 的提交消息片段在管道中插入额外的参数。我尝试使用以下代码来实现它,但没有成功:

variables:
  - name: commitMessage
    value: $(Build.SourceVersionMessage)
steps:
  - template: myTemplate.yaml
    parameters:
      ${{ if contains(variables['commitMessage'], 'SPECIAL_MESSAGE') }}:
        specialParam: 'someValue'
Run Code Online (Sandbox Code Playgroud)

在调查过程中,我发现这些表达式的表现有些出乎意料:

variables:
  - name: commitMessage
    value: $(Build.SourceVersionMessage)
steps:
  - bash: | 
      echo "${{ variables['commitMessage'] }}"
      echo "${{ contains(variables['commitMessage'], 'SPECIAL_MESSAGE') }}"
      echo "${{ contains('test SPECIAL_MESSAGE', 'SPECIAL_MESSAGE') }}"
Run Code Online (Sandbox Code Playgroud)

脚本输出是:

test SPECIAL_MESSAGE
False
True
Run Code Online (Sandbox Code Playgroud)

脚本的第一行正确输出提交消息。但是contains()脚本第二行中的函数似乎无法处理该变量,即使提交消息包含 SPECIAL_MESSAGE,表达式也会返回False

但是,如果我将变量设置为静态字符串而不是$(Build.SourceVersionMessage)变量,则表达式返回True,并且我可以添加额外的参数:

variables:
  - name: commitMessage
    value: 'test SPECIAL_MESSAGE'
Run Code Online (Sandbox Code Playgroud)

知道管道为什么会这样,或者如何让它工作吗?

JSO*_*ulo 1

文档说明了该变量Build.SourceVersionMessage

该变量仅在步骤级别可用,并且在作业或阶段级别中均不可用(即,在作业启动并签出代码之前不会提取消息)。

这意味着当涉及有条件插入步骤或参数时,不可能在编译时表达式中正确使用此变量。有一个功能请求要求此变量在编译时完全可用:https://developercommunity.visualstudio.com/t/post/10029833