Jenkins powershell插件总是成功构建

Bri*_*ian 19 powershell jenkins jenkins-plugins

我正在使用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
Run Code Online (Sandbox Code Playgroud)

我认为PowerShell的执行结果应该依赖于$lastexitcode.

这是PowerShell插件的错误吗?

Rob*_*son 10

对我来说,我希望脚本能够在Jenkins发生错误时立即停止并失败.这是通过将其添加到脚本的开头来完成的:

$ ErrorActionPreference ="停止"

这里讨论:如何在第一个错误上停止PowerShell脚本?


Chr*_*son 9

从1.3开始,插件不会处理异常,例如缺少命令的异常.你可以自己做try/catch:

try
{
    asdf
}
catch
{
    write-host "Caught an exception"
    exit 1
}
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅MSDN


rri*_*wer 5

根据插件的最新版本(2015 年 9 月 18 日 1.3 版),您必须使用 $LastExitCode 使构建失败。

版本 1.3(2015 年 9 月 18 日)

  • PowerShell 现在以非交互模式运行,以防止交互提示挂起构建
  • PowerShell 现在运行时 ExcecutionPolicy 设置为“绕过”以避免执行策略问题
  • 脚本现在以 $LastExitCode 退出,导致非零退出代码将构建标记为失败
  • 添加了可用环境变量的帮助和列表(包括英语和法语翻译)