简体   繁体   English

处理Jenkins Groovy脚本异常

[英]Handling Jenkins Groovy script exceptions

I am executing a C# exe, CRS.exe, that I expect to return a non-zero value, such as -1. 我正在执行C#exe CRS.exe,希望返回一个非零值,例如-1。 I am using ps to get the value back, and have this within a stage: 我正在使用ps取回值,并将其放在一个阶段中:

  try{            
      pcode = (powershell(returnStdout: true, script: 'return Invoke-Expression -Command \" .\\perfmon\\CRS.exe hello \"'))''
      echo "Pcode =  ${pcode} "
      }
  catch (err) {echo err.message }
  echo "Pcode =  ${pcode} "

Based on this post, "Normally, a script which exits with a nonzero status code will cause the step to fail with an exception." 根据这篇文章,“通常,以非零状态代码退出的脚本将导致该步骤失败并出现异常。” -- Jenkins pipeline bubble up the shell exit code to fail the stage I want to handle this non-zero result, is the exception handler the only way? -Jenkins管道使shell退出代码冒泡,使我要处理此非零结果的阶段失败 ,异常处理程序是唯一的方法吗? Results of above run: 以上运行结果:

    Running PowerShell script
tester arg = hello    
[Pipeline] echo
script returned exit code -1
[Pipeline] echo
Pcode =  null 

Interestingly enough, a char return seems to be fine? 有趣的是,返回char似乎还好吗? This returns without throwing an exception 这将返回而不会引发异常

 icode = (powershell(returnStdout: true, script: 'return Invoke-Expression -Command \'.\\perfmon\\zipInstaller.ps1 -urlString ' + fileContents + "'"))
                echo "icode =  ${icode} "

Results in 
[Pipeline] {
[Pipeline] powershell
[Chris] Running PowerShell script
[Pipeline] echo
icode =  -5

I would like to catch the return codes from the exe's and manage my groovy pipelines flow based on that. 我想从exe捕获返回代码并基于此管理我的常规管道流。 Any insight would be greatly appreciated. 任何见识将不胜感激。

Instead of using returnStdout: true , try using returnStatus: true . 代替使用returnStdout: true ,请尝试使用returnStatus: true It should always return the exit code. 它应始终返回退出代码。 Not as helpful if you need the output from the powershell command all at once (without returnStdout , it will just print output to the Jenkins log), but as a workaround for that you could pipe output to a file and then print that out. 如果您一次需要全部来自powershell命令的输出(没有returnStdout ,它只会将输出打印到Jenkins日志中)就没有帮助,但是作为一种解决方法,您可以将输出通过管道传输到文件,然后将其打印出来。

Another option (but it's ugly, so I don't recommend it) is to call Powershell from a bat command. 另一个选择(但是很丑陋,所以我不建议这样做)是从bat命令调用Powershell。 bat should mask the error code for the powershell command, so Jenkins won't get excited and fail automatically, and you'll still get stdout. bat应该掩盖powershell命令的错误代码,因此Jenkins不会兴奋并自动失败,并且您仍然会得到标准输出。 Obviously not too helpful if you actually wanted the error code though. 如果您实际上想要错误代码,显然不是太有用。 Your line would look something like 你的线看起来像

pcode = (bat(returnStdout: true, script: 'Powershell.exe "return Invoke-Expression -Command \" .\\perfmon\\CRS.exe hello \"'"))

That line will need a bit of refining, but it might be another option. 那条线将需要细化,但这可能是另一种选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM