[英]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.