如何在 TeamCity PowerShell 运行程序中传播错误 [英] How do I get errors to propagate in the TeamCity PowerShell runner

查看:16
本文介绍了如何在 TeamCity PowerShell 运行程序中传播错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TeamCity 7 构建配置,它几乎只是使用各种 TeamCity 参数调用 .ps1 脚本.

I have a TeamCity 7 Build Configuration which is pretty much only an invocation of a .ps1 script using various TeamCity Parameters.

我希望这可能是一个简单的设置问题:

I was hoping that might be a simple matter of setting:

  • 脚本

文件

  • 脚本文件

    %system.teamcity.build.workingDir%/Script.ps1

    %system.teamcity.build.workingDir%/Script.ps1

  • 脚本执行模式

    使用-File"执行 .ps1 脚本论证

    Execute .ps1 script with "-File" argument

  • 脚本参数

    %system.teamcity.build.workingDir% -OptionB %BuildConfigArgument% %BuildConfigArg2%

    %system.teamcity.build.workingDir% -OptionB %BuildConfigArgument% %BuildConfigArg2%

  • 然后我会期望:

    • 如果我弄乱了我的参数并且脚本无法启动,构建就会失败
    • 如果我的 Script.ps1 脚本抛出,则构建失败
    • 如果脚本 exits 的错误级别为非 0 我希望构建失败(也许这不是惯用的 PS 错误管理 - 应该只使用 .ps1在没有异常的情况下报告成功?)
    • if I mess up my arguments and the script won't start, the Build fails
    • if my Script.ps1 script throws, the Build fails
    • If the script exits with a non-0 Error Level I want the Build to Fail (maybe this is not idiomatic PS error management - should a .ps1 only report success by the absence of exceptions?)

    问题:这行不通.它应该如何工作?有什么我做错的事情可以通过选择不同的选项来解决吗?

    The question: It just doesn't work. How is it supposed to work? Is there something I'm doing drastically wrong that I can fix by choosing different options?

    推荐答案

    正如 the友好的 TeamCity 手册:

    将错误输出设置为Error并添加构建失败条件

    如果存在语法错误和异常,PowerShell 会将它们写入 stderr.要使 TeamCity 构建失败,请将错误输出"选项设置为错误"并添加构建失败条件,该条件将使构建失败的任何错误输出.

    In case syntax errors and exceptions are present, PowerShell writes them to stderr. To make TeamCity fail the build, set Error Output option to Error and add a build failure condition that will fail the build on any error output.

    完成这项工作的关键是更改两个默认值:

    The keys to making this work is to change two defaults:

    1. 构建失败条件的顶层,打开构建运行器记录了一条错误消息:
    2. 在[PowerShell] 构建步骤中,显示高级选项并设置错误输出:错误
    1. At the top level in the Build Failure Conditions, switch on an error message is logged by build runner:
    2. In the [PowerShell] Build Step, Show advanced options and set Error output: Error

    <小时>

    在 9.1 中,以下工作(如果它也适用于早期版本,我不会感到惊讶):


    In 9.1 the following works (I wouldn't be surprised if it works for earlier versions too):

    1. 使用默认选项创建 PowerShell 构建步骤
    2. 将下拉列表更改为脚本:源代码
    3. 添加一个 trap { Write-Error "Exception $_" ;在脚本顶部退出 98 }
    4. (对于适合 TeamCity 构建脚本的脚本类型,可选但更正确的 IMO)

    1. create a PowerShell Build Step with the default options
    2. change the dropdown to say Script: Source code
    3. Add a trap { Write-Error "Exception $_" ; exit 98 } at the top of the script
    4. (Optional but more correct IMO for the kind of scripting that's appropriate for within TeamCity build scripts)

    显示高级选项并打开选项:添加 -NoProfile 参数

    (可选,但对我来说这应该是默认设置,因为它可以更清晰地呈现正如@Jamal Mavadat 所建议的一>)

    (Optional, but for me this should be the default as it renders more clearly as suggested by @Jamal Mavadat)

    显示高级选项并打开错误输出:错误

    Show advanced options and switch on Error output: Error

    (ASIDE @JetBrains:如果标签是将 stderr 输出格式设置为",那么误导性会降低)

    (ASIDE @JetBrains: if the label was "Format stderr output as" it would be less misleading)

    这包括以下情况:

    1. 解析错误[冒泡为异常并立即停止执行]
    2. 异常 [thrown 在您的 PS 代码中直接或间接显示并触发退出代码以让 TC 停止构建]
    3. 脚本中的显式 exit n 传播到构建(如果非零则失败)
    1. Parse errors [bubble up as exceptions and stop execution immediately]
    2. Exceptions [thrown directly or indirectly in your PS code show and trigger an exit code for TC to stop the build]
    3. An explicit exit n in the script propagates out to the build (and fails it if non-zero)

    这篇关于如何在 TeamCity PowerShell 运行程序中传播错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆