Jenkins DSL作业:GroovyCastException无法转换对象

时间:2018-10-30 21:30:17

标签: jenkins groovy jenkins-job-dsl

我不熟悉groovy和Jenkins DSL工作。我正在尝试使用groovy脚本来创建运行测试脚本的作业。我遇到了无法投射对象的错误,不确定是什么问题以及在哪里抛出了投射。错误消息似乎没有指向特定的行,我以为类未导入可能是一个问题,但似乎是这样。看来该错误与JenkinsDSL文件有关。

下面是我创建的两个文件和错误消息。 JenkinsDSL.groovy包含一种采用参数来创建作业的方法。

提前谢谢!

更新 我通过以下网站进行了运行:http://job-dsl.herokuapp.com/,它确认问题出在JenkinsDSL.groovy

JenkinsDSL.groovy

class JenkinsDSL {
    String gitUrl, gitRemoteName, gitBranch, gitCredentials
    String project, testSuite, testCaseName, testCaseDesc

    def addJob(def dslFactory, String project, String testSuite, String testCaseName, String testCaseDesc) {

        dslFactory.folder("Selenium Tests") {
            displayName("Selenium Tests")
            description("someDescription")
        }

        dslFactory.listView(project) {
           description("Tests for ${project}")
            filterBuildQueue()
            filterExecutors()
            jobs { 
            }
            columns {
                status()
                weather()
                name()
                lastSuccess()
                lastFailure()
                lastDuration()
                buildButton()
            }
        }

        dslFactory.folder(testSuite) {
            displayName(testSuite)
            description("Verifies functionality related to ${testSuite}")
        }

        dslFactory.mavenJob("Selenium Tests/${project}/${testSuite}/${testCaseName}") {
            description("${testCaseDesc}")
            label('AWS-Linux-Java-7')
            wrappers {
                timeout {
                    absolute(5)
                }
            }
            scm {
                git {
                    remote {
                        name("${gitRemoteName}")
                        url("${gitUrl}")
                        credentials("${gitCredentials}")
                    }
                    branch("${gitBranch}")
                }
            }
            parameters {
                choiceParam('Browser', ['Chrome', 'Firefox', 'Safari'])
                choiceParam('Environment', ['Dev', 'Test'])
                choiceParam('TestCase', ["${testCaseName}"])
            }
            triggers {
                parameterizedCron('''
                    H 0 * * * % Browser=Chrome;Environment=Dev;TestCase=TestCase
                    H 0 * * * % Browser=Firefox;Environment=Dev;TestCase=TestCase
                    H 0 * * * % Browser=Safari;Environment=Dev;TestCase=TestCase
                    H 0 * * * % Browser=Chrome;Environment=Test;TestCase=TestCase
                    H 0 * * * % Browser=Firefox;Environment=Test;TestCase=TestCase
                    H 0 * * * % Browser=Safari;Environment=Test;TestCase=TestCase
                ''')
            }

            rootPOM("pom.xml")
            goals("clean test -DBrowser='$BROWSER' -DEnvironment='$ENVIRONMENT' -DTestCase='$TESTCASE'");

            publishers {
                archiveArtifacts {
                    pattern('target/surefire-reports/customized-emailable-report.html')
                    pattern('target/surefire-reports/emailable-report.html')
                    pattern('target/surefire-reports/index.html')
                    pattern('target/surefire-reports/screenshots/**')
                }
                wsCleanup {
                includePattern('target/surefire-reports/**')
                deleteDirectories(true)
                }
            }
        }
    }
}

PortalDSL.groovy

import JenkinsDSL;

def job = new JenkinsDSL(
    gitUrl: 'giturl',
    gitRemoteName: 'gitname',
    gitBranch: 'gitbranch',
    gitCredentials: 'gitcreds'
)

/********************************** Project 1 **********************************/

/***************** Test Suite 1 *****************/

/*** Test Case 1 ***/
job.addJob(
    this,
    'projectName',
    'testSuite',
    'testCase1',
    'Some Description'
)
/*** Test Case 2 ***/
job.addJob(
     this,
    'projectName',
    'testSuite',
    'testCase2',
    'Some Description'
)

Jenkins错误消息

Processing DSL script JenkinsDSL.groovy
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'org.codehaus.groovy.runtime.InvokerHelper$1@620c0d02' with class 'org.codehaus.groovy.runtime.InvokerHelper$1' to class 'javaposse.jobdsl.dsl.JobParent'
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:405)
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:319)
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:232)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:603)
at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScriptEngine(AbstractDslScriptLoader.groovy:103)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:384)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:69)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:174)
at javaposse.jobdsl.dsl.AbstractDslScriptLoader$_runScripts_closure1.doCall(AbstractDslScriptLoader.groovy:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
at groovy.lang.Closure.call(Closure.java:414)
at groovy.lang.Closure.call(Closure.java:430)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2040)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2025)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2078)
at org.codehaus.groovy.runtime.dgm$164.invoke(Unknown Source)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:274)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScripts(AbstractDslScriptLoader.groovy:46)
at javaposse.jobdsl.plugin.ExecuteDslScripts.perform(ExecuteDslScripts.java:323)
at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibilityLayer.java:81)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
at hudson.model.Build$BuildExecution.build(Build.java:206)
at hudson.model.Build$BuildExecution.doRun(Build.java:163)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:504)
at hudson.model.Run.execute(Run.java:1727)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
ERROR: Cannot cast object 'org.codehaus.groovy.runtime.InvokerHelper$1@620c0d02' with class 'org.codehaus.groovy.runtime.InvokerHelper$1' to class 'javaposse.jobdsl.dsl.JobParent'
Finished: FAILURE

1 个答案:

答案 0 :(得分:1)

似乎是由于我在Jenkins中的配置。在“处理作业DSL”中->在文件系统上查找-> DSL脚本,我放置了“ * .groovy”。相反,我应该放置了“ PortalDSL.groovy”

ellak指出我的代码中也包含Jenkins Pipeline,但是我更新了代码以不包含Pipeline语法。