[英]Jenkins groovy.lang.MissingPropertyException: No such property Bash
when running a Jenkins Groovy Piepline script that executes bash shell, a locally set variable can't be found when echo'ed out.在运行执行 bash shell 的 Jenkins Groovy Piepline 脚本时,回显时找不到本地设置的变量。
It reports error -它报告错误 -
groovy.lang.MissingPropertyException: No such property: md5Value for class: WorkflowScript
Any ideas / pointer would be much appreciated.任何想法/指针将不胜感激。
#!groovy
node {
try {
stage('Test-Echo') {
sh """
#!/usr/bin/env bash
md5Value='y'
echo 'md5Value : ${md5Value}'
"""
}
} catch (e) {
println (e.getMessage())
}
}
Yes I've tried these different permutations, all report the same error.是的,我尝试过这些不同的排列,都报告相同的错误。
echo 'md5Value : ${md5Value}'
echo "md5Value : ${md5Value}"
echo 'md5Value : ' ${md5Value}
echo "md5Value : " ${md5Value}
echo 'md5Value : $md5Value'
echo 'md5Value : ' $md5Value
echo "md5Value : $md5Value"
echo "md5Value : " $md5Value
If the exception is handled locally it produces error -如果异常在本地处理,则会产生错误 -
hudson.AbortException: script returned exit code 1
By updating the variable to通过将变量更新为
echo 'md5Value : " \${md5Value}
It's now producing error它现在产生错误
hudson.AbortException: script returned exit code 126 //: Permission denied
So the workaround is to put the bash shell into its own script and then the local variable assignment works as seen here -因此,解决方法是将 bash shell 放入其自己的脚本中,然后局部变量赋值的工作方式如下所示 -
#!groovy
node {
try {
stage('Test-Echo') {
git poll: true,
branch: "develop",
credentialsId: 'xxx',
url: 'https://yyy.org/here/and-there.git'
sh """
chmod +x ./jenkins/test/echo-test.sh
./jenkins/test/echo-test.sh
exit "\${?}"
"""
}
} catch (e) {
println (e.getMessage());
}
}
And the bash script和 bash 脚本
#!/bin/bash
md5Value="y"
echo "md5Value : ${md5Value}"
change the triple double quote to single quote, and replace the single quote to double quote inside the script as following:将三重双引号更改为单引号,并将脚本中的单引号替换为双引号,如下所示:
sh '''
#!/usr/bin/env bash
md5Value='y'
echo "md5Value : ${md5Value}"
'''
I've tried the workaround suggested on serverfault.com and it worked.我已经尝试了serverfault.com 上建议的解决方法,并且奏效了。 Try
试试
echo "md5Value : ${'$'}md5Value"
For me, this works well.对我来说,这很好用。
sh """
#!/usr/bin/env bash
md5Value='y'
echo md5Value : \$md5Value
"""
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.