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?
It contains a group of states called build, deploy, test and release. These events are interlinked with each other. Every state has its events, which work in a sequence called a continuous delivery pipeline.
Scripted Pipeline: Ideally, Scripted pipeline is written in Jenkins file on web UI of Jenkins. Unlike Declarative pipeline, the scripted pipeline strictly uses groovy based syntax.
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" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With