我正在使用Jenkins PowerShell插件来构建项目.
但是,我发现Jenkins总是认为我的构建成功,无论我在Windows PowerShell
命令内输入什么.
这是一个例子:
如您所见,asdf
这不是一个合法的命令.詹金斯应该FAILURE
在构建之后给我.
但控制台输出给了我:
Started by user admin Building in workspace C:\Users\Administrator\.jenkins\jobs\Test\workspace [workspace] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\Users\ADMINI~1\AppData\Local\Temp\hudson2092642221832331776.ps1'" The term 'asdf' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\Users\ADMINI~1\AppData\Local\Temp\hudson2092642221832331776.ps1:1 char:5 + asdf <<<< + CategoryInfo : ObjectNotFound: (asdf:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException Finished: SUCCESS
我认为PowerShell的执行结果应该依赖于$lastexitcode
.
这是PowerShell插件的错误吗?
对我来说,我希望脚本能够在Jenkins发生错误时立即停止并失败.这是通过将其添加到脚本的开头来完成的:
$ ErrorActionPreference ="停止"
这里讨论:如何在第一个错误上停止PowerShell脚本?
从1.3开始,插件不会处理异常,例如缺少命令的异常.你可以自己做try/catch
:
try { asdf } catch { write-host "Caught an exception" exit 1 }
有关更多信息,请参阅MSDN