Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 525
Peter McNab Avatar asked Oct 14 '16 18:10

Peter McNab


People also ask

What are the three fundamental stages of a Jenkins pipeline?

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.

Which type of Jenkins pipeline provides strict syntax?

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.


2 Answers

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() 
like image 157
Peter McNab Avatar answered Sep 23 '22 07:09

Peter McNab


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" } 
like image 37
Nakamura Avatar answered Sep 23 '22 07:09

Nakamura