UnsupportedOperationException:必须指定 $class 并实现接口 java.util.List

moa*_*ley 4 groovy jenkins

运行 Jenkinsfile 测试,下一阶段返回UnsupportedOperationException: must specify $class with an implementation of interface java.util.List

stage('Checkout') {
    steps {
        checkout([$class: 'GitSCM', 
            branches: [name: '*/master'], 
            doGenerateSubmoduleConfigurations: false, 
            extensions: [[$class: 'CleanBeforeCheckout'], 
                [$class: 'RelativeTargetDirectory', relativeTargetDir: 'targetDir']], 
            submoduleCfg: [], 
            userRemoteConfigs: [[credentialsId: 'jenkinsserviceaccount',
                url: 'https://bitbucket.company.net/scm/moak/myTestRepo.git']]])
    }
}
Run Code Online (Sandbox Code Playgroud)

为了在这里发布,我确实更改了一些名称,但上面的名称是使用 Jenkins 代码片段生成器创建的。您知道结帐时可能出现什么问题吗?

这是我在 Jenkins 控制台中看到的更多异常。

java.lang.UnsupportedOperationException: must specify $class with an implementation of interface java.util.List
    at org.jenkinsci.plugins.structs.describable.DescribableModel.resolveClass(DescribableModel.java:503)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:402)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.buildArguments(DescribableModel.java:341)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:282)
Caused: java.lang.IllegalArgumentException: Could not instantiate 
    {extensions=[{$class=CleanBeforeCheckout}, {$class=RelativeTargetDirectory, relativeTargetDir=stateStore}], 
    submoduleCfg=[], 
    userRemoteConfigs=[{credentialsId=jenkinsserviceaccount, url=https://bitbucket.hylandqa.net/scm/moak/hyland-statestore.git}], 
    doGenerateSubmoduleConfigurations=false, 
    branches={name=*/master}} 
for GitSCM(userRemoteConfigs: UserRemoteConfig(url: String, name: String, refspec: String, credentialsId: String)[], 
    branches: BranchSpec(name: String)[], 
    doGenerateSubmoduleConfigurations: boolean, 
    submoduleCfg: org.kohsuke.stapler.NoStaplerConstructorException: There's no @DataBoundConstructor on any constructor of class hudson.plugins.git.SubmoduleConfig[], 
    browser: GitRepositoryBrowser{AssemblaWeb(repoUrl: String) | BitbucketWeb(repoUrl: String) | CGit(repoUrl: String) | FisheyeGitRepositoryBrowser(repoUrl: String) | 
        GitBlitRepositoryBrowser(repoUrl: String, projectName: String) | GitLab(repoUrl: String, version: String) | GitList(repoUrl: String) | GitWeb(repoUrl: String) | 
        GithubWeb(repoUrl: String) | Gitiles(repoUrl: String) | GitoriousWeb(repoUrl: String) | GogsGit(repoUrl: String) | KilnGit(repoUrl: String) | 
        Phabricator(repoUrl: String, repo: String) | RedmineWeb(repoUrl: String) | RhodeCode(repoUrl: String) | Stash(repoUrl: String) | 
        TFS2013GitRepositoryBrowser(repoUrl: String) | ViewGitWeb(repoUrl: String, projectName: String)}, 
        gitTool: String, extensions: GitSCMExtension{AuthorInChangelog() | 
        BuildChooserSetting(buildChooser: BuildChooser{AncestryBuildChooser(maximumAgeInDays: int, ancestorCommitSha1: String) | 
        DefaultBuildChooser() | InverseBuildChooser()}) | ChangelogToBranch(options: ChangelogToBranchOptions(compareRemote: String, compareTarget: String)) | 
        CheckoutOption(timeout: int) | CleanBeforeCheckout() | CleanCheckout() | 
        CloneOption(shallow: boolean, noTags: boolean, reference: String, timeout: int, depth?: int, honorRefspec?: boolean) | 
        DisableRemotePoll() | GitLFSPull() | IgnoreNotifyCommit() | LocalBranch(localBranch: String) | MessageExclusion(excludedMessage: String) | 
        PathRestriction(includedRegions: String, excludedRegions: String) | PerBuildTag() | 
        PreBuildMerge(options: UserMergeOptions(mergeTarget: String, fastForwardMode?: GitPluginFastForwardMode[FF, FF_ONLY, NO_FF], mergeRemote?: String, mergeStrategy?: Strategy[DEFAULT, RESOLVE, RECURSIVE, OCTOPUS, OURS, SUBTREE, RECURSIVE_THEIRS])) | 
        PruneStaleBranch() | RelativeTargetDirectory(relativeTargetDir: String) | ScmName(name: String) | 
        SparseCheckoutPaths(sparseCheckoutPaths: SparseCheckoutPath(path: String)[]) | 
        SubmoduleOption(disableSubmodules: boolean, recursiveSubmodules: boolean, trackingSubmodules: boolean, reference: String, timeout: int, parentCredentials: boolean) | 
        UserExclusion(excludedUsers: String) | UserIdentity(name: String, email: String) | WipeWorkspace()}[])
Run Code Online (Sandbox Code Playgroud)

Szy*_*iak 5

您会看到此错误,因为brancheskey 需要保存嵌套在列表中的映射。所以应该是:

branches: [[name: '*/master']]
Run Code Online (Sandbox Code Playgroud)

反而

branches: [name: '*/master']
Run Code Online (Sandbox Code Playgroud)

来源:https://jenkins.io/doc/pipeline/steps/workflow-scm-step/#code-checkout-code-general-scm

完整的阶段如下所示:

stage('Checkout') {
    steps {
        checkout([$class: 'GitSCM', 
            branches: [[name: '*/master']], 
            doGenerateSubmoduleConfigurations: false, 
            extensions: [[$class: 'CleanBeforeCheckout'], 
                [$class: 'RelativeTargetDirectory', relativeTargetDir: 'targetDir']], 
            submoduleCfg: [], 
            userRemoteConfigs: [[credentialsId: 'jenkinsserviceaccount',
                url: 'https://bitbucket.company.net/scm/moak/myTestRepo.git']]])
    }
}
Run Code Online (Sandbox Code Playgroud)

关于您看到的错误 - 工作流插件使用 Groovy 的转换Map为另一种类型的能力。如果所有映射键都正确映射到类字段,例如它们必须存储相同的类型,那么它就可以工作。当branches存储 aMapList<Map>,整个映射无法转换为表示 SCM 配置的对象。