//获取标准输出
//第一种
result = sh returnStdout: true ,script: "<shell command>"
result = result.trim()
//第二种
result = sh(script: "<shell command>", returnStdout: true).trim()
//第三种
sh "<shell command> > commandResult"
result = readFile('commandResult').trim()

//获取执行状态
//第一种
result = sh returnStatus: true ,script: "<shell command>"
result = result.trim()
//第二种
result = sh(script: "<shell command>", returnStatus: true).trim()
//第三种
sh '<shell command>; echo $? > status'
def r = readFile('status').trim()

//无需返回值,仅执行shell命令
//最简单的方式
sh '<shell command>'

参考 :

jenkins pipeline中获取shell命令的标准输出或者状态

JENKINS-26133

how-to-execute-a-command-in-a-jenkins-2-0-pipeline-job-and-then-return-the-stdou

how-to-do-i-get-the-output-of-a-shell-command-executed-using-into-a-variable-fro

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐