Azure管道 - build.sourceversionMessage变量不起作用表达功能

发布于 2025-01-25 21:22:04 字数 1192 浏览 5 评论 0 原文

我正在尝试根据提交消息片段,运行Azure Devops Server 2020的插入管道中的额外参数。我试图用以下代码实现它,而没有运气:

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

在调查期间,我发现表达式的行为有些意外:

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

脚本输出为:

test SPECIAL_MESSAGE
False
True

脚本的第一行将正确输出提交消息。但是 contains()在脚本的第二行中函数似乎无法处理变量,即使提交消息包含special_message,expression返回 false

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

variables:
  - name: commitMessage
    value: 'test SPECIAL_MESSAGE'

知道管道为什么这样的行为或如何使其起作用?

I'm trying to insert an extra parameter in my pipeline based on a commit message fragment, running Azure DevOps Server 2020. I tried to implement it with the following code, without luck:

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

During investigation, I found that the expressions behave somewhat unexpected:

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

The script output is:

test SPECIAL_MESSAGE
False
True

The first line of the script outputs the commit message correctly. But the contains() function in the second line of the script seems to be unable to process the variable, even though the commit message contains the SPECIAL_MESSAGE, the expression returns False.

However, if I set my variable to a static string instead of the $(Build.SourceVersionMessage) variable, the expression returns True, and I am able to add the extra parameter:

variables:
  - name: commitMessage
    value: 'test SPECIAL_MESSAGE'

Any idea why the pipeline behaves like this, or how to make it work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抠脚大汉 2025-02-01 21:22:04

关于 build.sourceversionMessage 变量:

该变量仅在步骤级别上可用,并且在作业中既不可用,也不可在阶段级别中可用(即,在作业启动并检查了代码之前,该消息不会提取。

)。

这意味着在有条件地插入步骤或参数时,无法在编译时间表达式中正确使用此变量。有一个功能请求可以使此变量在编译时间充分可用:

The documentation states about the Build.SourceVersionMessage variable:

The variable is only available on the step level and is neither available in the job nor stage levels (i.e. the message is not extracted until the job had started and checked out the code).

This means that it's not possible to properly use this variable in compile time expressions, when it comes to conditionally inserting steps or parameters. There is a feature request to make this variable fully available at compile time: https://developercommunity.visualstudio.com/t/post/10029833

渔村楼浪 2025-02-01 21:22:04

您可以更改为如下:

variables:
- name: commitMessage
  value: ${{ variables['Build.SourceVersionMessage'] }}

表达式' $ {{variables ['build.sourceversionMessage']}}}}} '会将值传递给自定义变量' commitMessage '在编译时。这相当于在运行管道之前手动分配静态值。

表达式' $(build.sourceversionMessage) 是在运行时。

我已经用表达式' $ {{variables ['build.sourceversionMessage']}}} '进行了测试,它将能够按预期工作。

有关更多详细信息,您可以参考有关“ 理解变量语法 ”。

You can change to like as below:

variables:
- name: commitMessage
  value: ${{ variables['Build.SourceVersionMessage'] }}

The expression '${{ variables['Build.SourceVersionMessage'] }}' would pass the value to the custom variable 'commitMessage' at compile time. This is equivalent to manually assigning the static value before running the pipeline.

And the expression '$(Build.SourceVersionMessage)' is at runtime.

I have tested with the expression '${{ variables['Build.SourceVersionMessage'] }}', it would be able to work as expected.

For more details, you can reference the document about "Understand variable syntax".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文