繁体   English   中英

处理Jenkins Groovy脚本异常

[英]Handling Jenkins Groovy script exceptions

我正在执行C#exe CRS.exe,希望返回一个非零值,例如-1。 我正在使用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} "

根据这篇文章,“通常,以非零状态代码退出的脚本将导致该步骤失败并出现异常。” -Jenkins管道使shell退出代码冒泡,使我要处理此非零结果的阶段失败 ,异常处理程序是唯一的方法吗? 以上运行结果:

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

有趣的是,返回char似乎还好吗? 这将返回而不会引发异常

 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

我想从exe捕获返回代码并基于此管理我的常规管道流。 任何见识将不胜感激。

代替使用returnStdout: true ,请尝试使用returnStatus: true 它应始终返回退出代码。 如果您一次需要全部来自powershell命令的输出(没有returnStdout ,它只会将输出打印到Jenkins日志中)就没有帮助,但是作为一种解决方法,您可以将输出通过管道传输到文件,然后将其打印出来。

另一个选择(但是很丑陋,所以我不建议这样做)是从bat命令调用Powershell。 bat应该掩盖powershell命令的错误代码,因此Jenkins不会兴奋并自动失败,并且您仍然会得到标准输出。 如果您实际上想要错误代码,显然不是太有用。 你的线看起来像

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

那条线将需要细化,但这可能是另一种选择。

暂无
暂无

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

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