团队城市。命令行构建步骤。 setParameter 总是显示 "Incorrect property name" 错误

Teamcity. Command line build step. setParameter always show "Incorrect property name" error

我正在尝试使用命令行构建步骤填充一些参数。

代码如下:

#!/bin/bash -x
VERSIONCODE=123 
VERSIONNAME=1.2.0
echo "##teamcity[setParameter name='env.VERSION_NAME' value='$VERSIONNAME']" 
echo "##teamcity[setParameter name='env.VERSION_CODE' value='$VERSIONCODE']"

构建日志:

[09:14:06][Step 1/8] + VERSIONCODE=123 
[09:14:06][Step 1/8] + VERSIONNAME=1.2.0 
[09:14:06][Step 1/8] + echo ' 
[09:14:06][Step 1/8] ##teamcity[setParameter name='\''env.VERSION_NAME'\'' value='\''1.2.0'\''] 
[09:14:06] 
[Step 1/8] Incorrect property name. 
Valid property list format is (name( )*=( )*'escaped_value'( )*)* where escape symbol is "|" 
[09:14:06][Step 1/8] ' 
[09:14:06][Step 1/8] + echo ' 
[09:14:06][Step 1/8] ##teamcity[setParameter name='\''env.VERSION_CODE'\'' value='\''123'\''] 
[09:14:06] 
[Step 1/8] Incorrect property name. 
Valid property list format is (name( )*=( )*'escaped_value'( )*)* where escape symbol is "|" 
[09:14:06][Step 1/8] '

我已经尝试 google 几个小时了,但一无所获。 我做错了什么?

我在 teamcity-support.jetbrains.com 上问过,得到了答案:

The issue is with the "-x" from the script definition. It seems to go to extra lengths for tracing the script, which ends in the script failing. Removing the -x will make the script work properly. You can verify that the parameters are set at the end of the build by checking on the build result page, parameters tab.

有效。

-x bash 选项非常有用,解决方案不需要删除该选项。相反,让我们看看为什么问题首先出现在自定义脚本中。

使用 bash 的 -x 选项,bash 将回显即将执行的行,包括文字转义字符 。 TeamCity 将尝试解析此输出(此时我们不希望这样做,因为命令行回显不打算被解析)。如果 TC 发现回显命令行格式错误(例如,由于转义字符),它将发出 Incorrect property name. 警告:

echo "##teamcity[buildStatisticValue key=\'warnings\' value=\'42\']"

但是,TC 会看到该命令的执行输出正常。

如果 TC 将回显的命令行正确解析为 TC 消息,情况会更糟,因为它会在该行执行时再次解析它,可能导致 double-counting 和其他细微错误。

相反,如果您的自定义脚本中必须有 ## 并且 -x 已启用,请务必像这样转义两个哈希值:

echo \#\#teamcity[buildStatisticValue key=\'warnings\' value=\'42\']

Bash 将打印带有转义哈希的确切命令行,TC 不会尝试解析它(并且失败或 double-count),但是当该行被执行时它会回显作为 ##teamcity[buildStatisticValue key='warnings' value='42' TC 将按预期进行解析。