我正在尝试根据提交消息片段,运行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?
发布评论
评论(2)
关于
build.sourceversionMessage
变量:这意味着在有条件地插入步骤或参数时,无法在编译时间表达式中正确使用此变量。有一个功能请求可以使此变量在编译时间充分可用:
The documentation states about the
Build.SourceVersionMessage
variable: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
您可以更改为如下:
表达式'
$ {{variables ['build.sourceversionMessage']}}}}}
'会将值传递给自定义变量'commitMessage
'在编译时。这相当于在运行管道之前手动分配静态值。表达式'
$(build.sourceversionMessage)
是在运行时。我已经用表达式'
$ {{variables ['build.sourceversionMessage']}}}
'进行了测试,它将能够按预期工作。有关更多详细信息,您可以参考有关“ 理解变量语法 ”。
You can change to like as below:
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".