本文介绍了TeamCity 8 构建步骤中的 PowerShell 退出代码始终为 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用的是 TeamCity Enterprise 8.0.5.

我有一个运行 PowerShell (.ps1) 脚本的 TeamCity 构建步骤,如下所示:

试试{# 打破一些东西$a = 1/0}抓住{出口 1}

尽管如此,在构建日志中,该步骤成功并以代码 0 退出.

[10:02:18][Step 2/3] 进程退出,代码为 0

如果脚本中有任何失败,我希望该步骤失败.我怎样才能做到这一点?

解决方案

我刚刚发现这个帖子:

其次,在构建失败条件屏幕中,确保构建运行器记录了错误消息被选中:

We are using TeamCity Enterprise 8.0.5.

I have a TeamCity build step which runs a PowerShell (.ps1) script, which looks like this:

try
{
    # Break something
    $a = 1 / 0
}
catch
{
    Exit 1
}

Despite this, in the build log, the step succeeds and exits with code 0.

I want the step to fail if there are any failures in the script. How can I make this happen?

解决方案

I have just discovered this post:

PowerShell runner - script fails but the build succeeds - 'Process exited with code 0'

There is a bug in TeamCity which means that non-zero PowerShell return codes are not picked up.

The solution suggested is to create a build failure condition on detection of certain text output into the build log.

However, my solution involved something different.

In the catch block you only have to use the Write-Error cmdlet:

catch
{
    Write-Error -Exception $_.Exception
}

Then you must ensure that two things are set correctly in TeamCity:

Firstly, the build step Error Output should be set to error, and not warning:

Secondly, in the build failure conditions screen, make sure an error message is logged by build runner is checked:

这篇关于TeamCity 8 构建步骤中的 PowerShell 退出代码始终为 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 01:27