Jenkins的Groovy字符串连接问题

Jenkins的Groovy字符串连接问题,groovy,jenkins,Groovy,Jenkins,我有一个非常简单的Groovy脚本: String test = ""; [0, 1, 2, 3, 4].each {test += it.toString()} return test 当我在Jenkins脚本控制台中输入并执行脚本时,我得到了预期的结果: Result: 01234 但是,当我使用curl执行相同的脚本时,我会得到一些其他结果: curl -X POST -d "script=$(cat test.groovy)" http://my/domain/jenkins/scr

我有一个非常简单的Groovy脚本:

String test = "";
[0, 1, 2, 3, 4].each {test += it.toString()}
return test
当我在Jenkins脚本控制台中输入并执行脚本时,我得到了预期的结果:

Result: 01234
但是,当我使用curl执行相同的脚本时,我会得到一些其他结果:

curl -X POST -d "script=$(cat test.groovy)" http://my/domain/jenkins/scriptText
Result: 4

有人知道这种差异是从哪里来的吗?

不确定出了什么问题,但转换成显式字符串对我来说很有用:

curl --show-error -d 'script=test="";[0,1,2,3,4].each{test = "${test}${it}"}; return test' http://my/domain/jenkins/scriptText
Result: 01234
分解您的示例,并添加适当的调试,可以:

$ curl --show-error -d 'script=test="";[0,1,2,3,4].each{test += it.toString(); println test}; return test' http://my/domain/jenkins/scriptText
0
1
2
3
4
Result: 4
因此,测试没有作为字符串添加到。使用gstring直接将其分类

另一种方法是使用inject,尽管它仍然使用gstring:

$ curl --show-error -d 'script=test=[0,1,2,3,4].inject("") {acc, val -> "${acc}${val}"}; return test' http://my/domain/jenkins/scriptText
Result: 01234