gradle 在 Jenkins 作业 DSL 定义中使用 slackNotifier 时测试失败

gradle test fail when using slackNotifier in Jenkins Job DSL definition

更新: 从自动生成的 DSL wiki 条目底部 ... The generated DSL is only supported when running in Jenkins,...

由于 slackNotifier 是生成的 DSL,因此似乎没有办法在我们的特定基础架构中对其进行测试。我们将编写一个使用配置块生成配置的函数。


我有一个 seed job definition 失败了 gradle test 即使我们在 Jenkins 中使用它时它似乎工作正常。

Job Definition 摘录

//package master
// GitURL
def gitUrl = 'https://github.com/team/myapp'
def slackRoom = null

job('seed-dsl') {
    description('This seed is updated from the seed-dsl-updater job')
    properties {
        //Set github project URL
        githubProjectUrl(gitUrl)
    }
    ...
    // publishers is another name for post build steps
    publishers {
        mailer('', false, true)
        slackNotifier {
            room(slackRoom)
            notifyAborted(true)
            notifyFailure(true)
            notifyNotBuilt(true)
            notifyUnstable(true)
            notifyBackToNormal(true)
            notifySuccess(false)
            notifyRepeatedFailure(false)
            startNotification(false)
            includeTestSummary(false)
            includeCustomMessage(false)
            customMessage(null)
            buildServerUrl(null)
            sendAs(null)
            commitInfoChoice('NONE')
            teamDomain(null)
            authToken(null)
        }
    }
}

当我用 slackNotifier 声明注释掉时,gradle test 命令工作正常,但在启用时失败并出现以下错误:

Test output 节选

Caused by:
        javaposse.jobdsl.dsl.DslScriptException: (script, line 79) No signature of method: javaposse.jobdsl.dsl.helpers.publisher.PublisherContext.slackNotifier() is applicable for argument types: (script$_run_closure1$_closure9$_closure14) values: [script$_run_closure1$_closure9$_closure14@d2392a1]
        Possible solutions: stashNotifier(), stashNotifier(groovy.lang.Closure)
            at javaposse.jobdsl.dsl.DslScriptLoader.runScriptEngine(DslScriptLoader.groovy:135)
            at javaposse.jobdsl.dsl.DslScriptLoader.runScriptsWithClassLoader_closure1(DslScriptLoader.groovy:78)

根据 migration doc, slackNotifer has been supported since 1.47. In my gradle.build,我使用的是 1.48。我在插件版本 1.50

中看到了同样的错误

gradle.build 节选

ext {
 jobDslVersion = '1.48'
 ...
}
...
// Job DSL plugin including plugin dependencies
testCompile "org.jenkins-ci.plugins:job-dsl:${jobDslVersion}"
testCompile "org.jenkins-ci.plugins:job-dsl:${jobDslVersion}@jar"
...

gradle.build 还包括以下内容,如 [测试文档] *(https://github.com/jenkinsci/job-dsl-plugin/wiki/Testing-DSL-Scripts).

testPlugins 'org.jenkins-ci.plugins:slack:2.0.1'

我需要做什么才能成功测试我的工作定义。这是一个错误,还是我遗漏了什么?

删除了不正确的回复


编辑

我明白我没抓住要点。

新方法是重用插件公开的@DataBoundConstructor,因此无需编写任何内容来支持具有 DataBoundConstructor 的新插件

您的 SlackNotifier 有这个 - 请注意 DSL 会为您转换小写首字母

@DataBoundConstructor
public SlackNotifier(
    final String teamDomain, 
    final String authToken, 
    final String room, 
    final String buildServerUrl,
    final String sendAs, 
    final boolean startNotification, 
    final boolean notifyAborted, 
    final boolean notifyFailure,
    final boolean notifyNotBuilt, 
    final boolean notifySuccess, 
    final boolean notifyUnstable, 
    final boolean notifyBackToNormal,
    final boolean notifyRepeatedFailure, 
    final boolean includeTestSummary, 
    CommitInfoChoice commitInfoChoice,
    boolean includeCustomMessage, 
    String customMessage) {
    ...
}

不幸的是,参数列表中有一个嵌入式类型 CommitInfoChoice,它没有 DataBoundConstructor,它也没有 enum

public enum CommitInfoChoice {
    NONE("nothing about commits",                             false, false),
    AUTHORS("commit list with authors only",                  true,  false),
    AUTHORS_AND_TITLES("commit list with authors and titles", true,  true);
    ...
}

我会冒昧地说,在嵌套枚举实现数据绑定构造函数并且还有一个描述符之前,它不会解决这个问题,抱歉。

我没有该插件,但您可以查看 XML 以了解使用该插件创建的真实作业,并查看本节的内容。我怀疑是嵌套结构

您可以尝试 job dsl google group - link 到 post 关于通用方法

我们运行也参与其中。我们的解决方案是将我们在 jenkins 上使用的 slack 插件版本添加到 gradle.

中的插件列表中

更具体地说,在依赖项下的 build.gradle 文件中,我们添加了以下代码以包含我们的插件,从而允许自动生成的 DSL 工作。

您可以在此处查看此描述以及 testPlugins 旁边的不同插件示例:

喜欢以下内容:

dependencies {
  ...
  // plugins to install in test instance
  testPlugins 'org.jenkins-ci.plugins:ghprb:1.31.4'
  testPlugins 'com.coravy.hudson.plugins.github:github:1.19.0'
}