Jenkins Groovy 管道,变量添加换行符并中断 curl 语句

Jenkins Groovy pipeline, variable adds a newline and breaks curl statement

我有一份 jenkins 工作,部分代码如下所示:

def version = sh returnStdout: true, script: """ #!/bin/sh                    
find  src/*/*/ -name *.ear |  grep -Eo \'[0-9]+\.[0-9]+\.[0-9]+\' 
"""
sh "echo $version"


def imageVersion = sh returnStdout: true, script: """
#!/bin/sh                    
curl -k -H 'X-JFrog-Art-Api:${api_key}'  https://artifactory.xxx.com/api/search/latestVersion\?g=com.xxx.bwce&a=billing-app-v1&v=$version-SNAPSHOT&repos=libs-snapshot-local
"""

而且我注意到在执行过程中,这个 curl 语句在这个变量替换处被破坏了。

这里是 Jenkins 控制台日志的输出结果。

+ echo 1.0.6
1.0.6
[Pipeline] sh

+ curl -k -H X-JFrog-Art-Api:**** 'https://artifactory.xxx.com/api/search/latestVersion?g=com.xxx.bwce&a=billing-app-v1&v=1.0.6'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100    23    0    23    0     0     34      0 --:--:-- --:--:-- --:--:--    34
+ '-SNAPSHOT&repos=libs-snapshot-local'

请建议是否有解决此问题的方法。我不确定为什么它在 curl 语句中添加换行符,但 echo 显示其中没有换行符。

您可以 trim 通过添加行

version 末尾换行
version = version.trim()

在你的 def version = 行之后