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

Jenkins DSL作业:GroovyCastException无法强制转换对象,jenkins,groovy,jenkins-job-dsl,Jenkins,Groovy,Jenkins Job Dsl,我是groovy和Jenkins DSL工作的新手。我正在尝试使用groovy脚本来创建运行测试脚本的作业。我遇到了无法强制转换对象错误,不确定问题是什么以及强制转换在哪里被取消。错误消息似乎没有指向特定的行,但我认为这可能是类未导入的问题,但似乎是这样。这个错误似乎与JenkinsDSL文件有关 下面是我创建的两个文件和错误消息。groovy包含一个方法,该方法接受参数来创建作业 提前谢谢你 更新 我在这个网站上运行了它:它确认了这个问题在JenkinsDSL.groovy中 JenkinsD

我是groovy和Jenkins DSL工作的新手。我正在尝试使用groovy脚本来创建运行测试脚本的作业。我遇到了无法强制转换对象错误,不确定问题是什么以及强制转换在哪里被取消。错误消息似乎没有指向特定的行,但我认为这可能是类未导入的问题,但似乎是这样。这个错误似乎与JenkinsDSL文件有关

下面是我创建的两个文件和错误消息。groovy包含一个方法,该方法接受参数来创建作业

提前谢谢你

更新 我在这个网站上运行了它:它确认了这个问题在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)
                }
            }
        }
    }
}
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'
)
PortalDSL.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)
                }
            }
        }
    }
}
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

看起来可能是因为我在詹金斯的配置。在流程作业DSL“->查看文件系统->DSL脚本中,我放了“*.groovy”。相反,我应该放“PortalDSL.groovy”


ellak指出,我的代码中也有Jenkins管道,但我已经更新了代码,使其不包含管道语法。

看起来您的代码中混合了Jenkins Job DSL和Jenkins Pipeline。请在此处阅读Job DSL:感谢您捕捉到这一点。我已经进行了编辑,试图在代码中删除管道,但我仍然没有找到它这是一个类似的错误。