jenkins执行shell文件_如何从Jenkinsfile(groovy)中获取使用执...

jenkins执行shell文件_如何从Jenkinsfile(groovy)中获取使用执...

2023年8月2日发(作者:)

jenkins执⾏shell⽂件_如何从Jenkinsfile(groovy)中获取使⽤执。。。If you want to get the stdout AND know whether the command succeeded or not, just use returnStdout and wrap it in anexception handler:scripted pipelinetry {// Fails with non-zero exit if dir1 does not existdef dir1 = sh(script:'ls -la dir1', returnStdout:true).trim()} catch (Exception ex) {println("Unable to read dir1: ${ex}")}output:[Pipeline] sh[Test-Pipeline] Running shell script+ ls -la dir1ls: cannot access dir1: No such file or directory[Pipeline] echounable to read dir1: xception: script returned exit code 2Unfortunately xception is missing any useful method to obtain that exit status, so if the actual value isrequired you'd need to parse it out of the message (ugh!)Update:If you also want the STDERR output from the shell command, there could be a couple of possible approaches:a) Redirect STDERR to STDOUT 2>&1- but it's then up to you to parse that out of the main output though, and you won't get the output if the command failed -because you're in the exception handler.b) redirect STDERR to a temporary file (the name of which you prepare earlier) 2>filename (but remember to clean up thefile afterwards) - ie. main code becomes:def stderrfile = ''try {def dir1 = sh(script:"ls -la dir1 2>${stderrfile}", returnStdout:true).trim()} catch (Exception ex) {def errmsg = readFile(stderrfile)println("Unable to read dir1: ${ex} - ${errmsg}")}c) Go the other way, set returnStatus=true instead, dispense with the exception handler and always capture output to a file,ie:def outfile = ''def status = sh(script:"ls -la dir1 >${outfile} 2>&1", returnStatus:true)def output = readFile(outfile).trim()if (status == 0) {// output is directory listing from stdout} else {// output is error message from stderr}Caveat: the above code is Unix/Linux-specific - Windows requires completely different shell commands.

发布者:admin,转转请注明出处:http://www.yc00.com/web/1690917316a463022.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信