如何将 svn 命令输出分配给 groovy 脚本中的变量

How to assign svn command output to a variable in groovy script

所以我有一个这样的 groovy 脚本-

def BRANCH_REV = sh script: "svn info $url | grep 'Last Changed Rev' | awk '{ print $4; }'", returnStdout: true , trim: true

println "ccsmp - downstream testing for: $BRANCH_REV"

这个脚本假设获取最后的修订号并将该值放入一个名为“BRANCH_REV”的变量中,第二行假设打印它。 但是我一直得到空值,我的控制台输出看起来像这样-

[branches%2Ffeatures%2Fcre40_jenkins] Running shell script
+ svn info
+ grep 'Last Changed Rev'
+ awk '{ print ; }'
23401               <-- It is printing the revision number here

[Pipeline] echo
ccsmp - downstream testing for: 0      <-- but when I try to print the $BRANCH_REV, it prints out 0

[Pipeline] sh
[branches%2Ffeatures%2Fcre40_jenkins] Running shell script
+ echo 0
0                                    <-- same thing happens when I try to echo $BRANCH_REV

解决方案

如果您的脚本在 Jenkinsfile 中并且您的管道配置为检出该 Jenkinsfile(即 Jenkinsfile 与您的代码一起存储),您只需执行以下操作:

def BRANCH_REV = "r${SVN_REVISION}"
echo "Revision is ${BRANCH_REV}"