如何在TeamCity PowerShell转轮中传播错误 [英] How do I get errors to propagate in the TeamCity PowerShell runner

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

问题描述

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



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




  • 脚本


    文件



  • strong>脚本文件


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



  • 脚本执行模式


    使用-File参数执行.ps1脚本



  • 脚本参数


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





然后我会期待:




  • 如果我搞砸我的论据,脚本不会启动,Build失败

  • 如果我的Script.ps1脚本抛出,Build失败

  • 如果脚本退出 0 错误级别我想要构建失败(也许这不是惯用的PS错误管理 - 应该.ps1只报告没有异常的成功?)






问题:它不起作用它应该如何工作?有没有什么我正在做的错误,我可以通过选择不同的选项来修复?

解决方案

href =https://confluence.jetbrains.com/display/TCD9/PowerShell =nofollow noreferrer>友好的TeamCity手册:


设置错误输出到错误 并添加构建失败条件



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


执行此工作的关键是更改两个默认值:


  1. 构建失败条件,打开 构建运行程序记录错误消息:

  2. 在[PowerShell ] 构建步骤,显示高级选项并设置错误输出:错误






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


  1. 使用默认选项创建PowerShell构建步骤

  2. 将下拉列表更改为说出脚本:源代码

  3. 添加一个陷阱{Write-ErrorException $ _;

  4. (可选但更为正确的IMO适用于TeamCity构建脚本中适用的脚本) / p>

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


  5. (可选,但是对于我来说,这应该是默认的,因为它更清楚地显示出根据@Jamal Mavadat的建议) p>

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



    (ASIDE @JetBrains:如果标签是格式stderr输出为,那么这个误会会更少)


这包括以下情况:


  1. 解析错误[冒泡为异常并立即停止执行]

  2. 例外[直接或间接地在您的PS代码中显示并触发TC的退出代码以停止构建]

  3. 显式<$脚本中的c $ c> exit n 传播到构建(如果不为零则失败)


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:

  • Script

    File

  • Script File

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

  • Script execution mode

    Execute .ps1 script with "-File" argument

  • Script arguments

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

And then I would expect:

  • 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?

解决方案

As doc'd in the friendly TeamCity manual:

Setting Error Output to Error and adding build failure condition

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. 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


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

  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)

    Show advanced options and switch on Options: Add -NoProfile argument

  5. (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: if the label was "Format stderr output as" it would be less misleading)

This covers the following cases:

  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天全站免登陆