在 Jenkinsfile 中执行 "Publish TestNG results" 步骤

Execute "Publish TestNG results" step in Jenkinsfile

我正在尝试将一组现有的 Jenkins 作业转换为新的管道语法,但我不明白如何转换 post-build 操作 "Publish TestNG results"。管道语法页面没有帮助,因为未列出此特定操作,因此我尝试了以下语法:

step([$class: 'hudson.plugins.testng.Publisher', reportFilenamePattern: 'test-output/testng-results.xml'])

我的假设是 class 名称必须与当前配置中 config.xml 的内容相匹配:

<hudson.plugins.testng.Publisher plugin="testng-plugin@1.10">
  <reportFilenamePattern>test-output/testng-results.xml</reportFilenamePattern>
  <escapeTestDescp>true</escapeTestDescp>
  <escapeExceptionMsg>true</escapeExceptionMsg>
  <showFailedBuilds>false</showFailedBuilds>
  <unstableOnSkippedTests>false</unstableOnSkippedTests>
  <failureOnFailedTestConfig>false</failureOnFailedTestConfig>
</hudson.plugins.testng.Publisher>

但是,执行语句时抛出异常:

java.lang.IllegalArgumentException: argument type mismatch
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
  at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:193)
  at org.jenkinsci.plugins.workflow.steps.StepDescriptor.newInstance(StepDescriptor.java:104)
  at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:134)
  at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:113)
  at groovy.lang.GroovyObject$invokeMethod.call(Unknown Source)
  at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
  at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
  at org.kohsuke.groovy.sandbox.impl.Checker.call(Checker.java:151)
  at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:21)
  at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:115)
  at org.kohsuke.groovy.sandbox.impl.Checker.call(Checker.java:149)
  at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:146)
  at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:123)
  at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:123)
  at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:15)
  at WorkflowScript.testBranch(WorkflowScript:71)

所以我的问题是:

如果这与问题相关,我是 运行 Jenkins 2.8,所有管道插件都是最新的,并且可以找到完整的 Jenkinsfile on GitHub

目前,TestNG 插件与管道不兼容。参见 JENKINS-27121

现在可以在 Pipelines 中发布 TestNG 测试结果。 使用:

step([$class: 'Publisher'])

或结果文件的自定义位置:

step([$class: 'Publisher', reportFilenamePattern: '**/custom/testng-results.xml'])

根据 this comment on Jenkins Jira 添加到 Pipeline 片段生成器的过程正在等待中。

(在 Jenkins 2.9.13 和 Pipeline 插件 2.4 上测试)