在Jenkins Job DSL定义

时间:2016-09-02 19:10:19

标签: jenkins jenkins-job-dsl

更新 从自动生成的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,自1.47以来一直支持slackNotifer。在我的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'

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

2 个答案:

答案 0 :(得分:0)

删除了错误回复

修改

我看到我错过了这一点。

新方法是重用插件公开的@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 - 链接到有关通用方法的帖子

答案 1 :(得分:0)

我们也碰到了这个。我们的解决方案是将我们在jenkins上使用的松弛插件版本添加到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'
}
相关问题