如何将值从一个Shell块传递到另一个Shell区块,或将该值分配给Jenkins中Groovy脚本的变量


script{
println("Starting TEST")
//Declaring Variable var value in the Below Shell Block
sh"""
echo "This is Test"
var=10
echo "This is Test Value $var"
"""
//Trying to Print the var Value from above Block
sh"""
echo "This is Other Block"
echo "This is Test Value $var"
"""
//Copying the value from Shell Script Variable to Groovy Script
def scriptVar=sh(script:"$var ")
}

在上面的代码中,Im试图打印外壳块的值,并使用相同的值在下一个块中分配值,但在第二个块中打印值后,echo将只显示空白消息

您只需将值写入文件,然后从中读取即可。检查以下内容。

script{
println("Starting TEST")
//Declaring Variable var value in the Below Shell Block
sh"""
echo "This is Test"
var=10
echo $var > varOut
echo "This is Test Value $var"
"""

//Trying to Print the var Value from above Block
sh"""
echo "This is Other Block"
var=$(cat varOut)
echo "This is Test Value $var"
"""
// Assign the value to a variable
def var = readFile(file: "varOut")
echo "Var = $var"
}

最新更新