简体   繁体   English

TeamCity 8 构建步骤中的 PowerShell 退出代码始终为 0

[英]PowerShell exit code is always 0 in TeamCity 8 build step

We are using TeamCity Enterprise 8.0.5.我们正在使用 TeamCity Enterprise 8.0.5。

I have a TeamCity build step which runs a PowerShell (.ps1) script, which looks like this:我有一个运行 PowerShell (.ps1) 脚本的 TeamCity 构建步骤,如下所示:

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

Despite this, in the build log, the step succeeds and exits with code 0.尽管如此,在构建日志中,该步骤成功并以代码 0 退出。

[10:02:18][Step 2/3] Process exited with code 0 [10:02:18][步骤 2/3] 进程退出,代码为 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' PowerShell 运行程序 - 脚本失败但构建成功 - '进程退出,代码为 0'

There is a bug in TeamCity which means that non-zero PowerShell return codes are not picked up. TeamCity 中存在一个错误,这意味着不会拾取非零的 PowerShell 返回代码。

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 cmdlet:

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

Then you must ensure that two things are set correctly in TeamCity:然后您必须确保在 TeamCity 中正确设置了两件事:

Firstly, the build step Error Output should be set to error , and not warning :首先,构建步骤Error Output应该设置为error ,而不是warning

在此处输入图片说明

Secondly, in the build failure conditions screen, make sure an error message is logged by build runner is checked:其次,在构建失败条件屏幕中,确保检查构建运行器记录的错误消息

在此处输入图片说明

Suppose you are using psake and your goal is to fail your build if your build script fails.假设您正在使用psake,并且您的目标是在构建脚本失败时使构建失败。 The script which imports the psake module and invokes the build script does not fail, so TeamCity takes it as a successful build.导入 psake 模块并调用构建脚本的脚本不会失败,因此 TeamCity 将其视为成功构建。

Add this code into your first script to fail your build if the second script fails.将此代码添加到您的第一个脚本中,如果第二个脚本失败,则构建失败。

Import-Module .\psake\psake.psm1

Invoke-Psake .\build-steps.ps1 @args

if($psake.build_success -eq $false){
    Write-Host "There was an error running psake script"
    exit 1
}
Remove-Module psake

Do not remove the module before the if statement.不要在if语句之前删除模块。

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

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