简体   繁体   中英

Jenkins pipeline script fails with “General error during class generation: Method code too large!”

When running a large Jenkins pipeline script, it can give the error:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: General error during class generation: Method code too large!

java.lang.RuntimeException: Method code too large!

What is the reason for this error and how can it be fixed?

This is due to a limit between Java and Groovy, requiring that method bytecode be no larger than 64kb. It is not due to the Jenkins Pipeline DSL.

To solve this, instead of using a single monolithic pipeline script, break it up into methods and call the methods.

For example, instead of having:

stage foo
parallel([
 ... giant list of maps ...
])

Instead do:

stage foo
def build_foo() {
  parallel([
     ...giant list of maps...
  ])}
build_foo()

If you are using declarative pipeline with shared library , you may need to refactor and externalize your global variables in the new methods. Here is a full example:

Jenkinsfile:

@Library("my-shared-library") _
myPipeline()

myPipeline.groovy:

def call() {
    String SOME_GLOBAL_VARIABLE
    String SOME_GLOBAL_FILENAME
    pipeline {
        stages() {
            stage('stage 1') {
                steps {
                    script {
                        SOME_GLOBAL_VARIABLE = 'hello'
                        SOME_GLOBAL_FILENAME = 'hello.txt'
                        ...
                    }
                }
            }
            stage('stage 2') {
                steps {
                    script {
                        doSomething(fileContent: SOME_GLOBAL_VARIABLE, filename: SOME_GLOBAL_FILENAME)
                        sh "cat $SOME_GLOBAL_FILENAME"
                    }
                }
            }
        }
    }
}
def doSomething(Map params) {
    String fileContent = params.fileContent
    String filename = params.filename
    sh "echo $fileContent > $filename"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM