Ethan McTague
Ethan McTague

Reputation: 2358

Can I speed up Gradle daemon startup on Jenkins CI?

Every time I push my gradle build to Jenkins, it spends a considerable amount of time on this step:

Starting a Gradle Daemon (subsequent builds will be faster)

The relevant part of my Jenkinsfile looks like this:

stage('Build') {
  steps {
    withGradle() {
      sh 'chmod +x gradlew'
      sh './gradlew build jar'
    }
  }
}

I assumed withGradle() would try to persistently run a gradle daemon in the background on Jenkins to avoid this sort of thing, but at this point i'm not entirely sure it does anything - the docs for it are incredibly vague.

How do I improve build times with this system?

Upvotes: 0

Views: 2611

Answers (1)

Thomas K.
Thomas K.

Reputation: 6770

withGradle is contributed by Jenkins' Gradle Plugin and contributes console output highlighting and build scan URL capturing (showing the build scan URL in the Jenkins UI). It certainly doesn't do anything with the Gradle daemon. You do not need withGradle to run you Gradle builds in Jenkins, depending whether you use build scans of course. Doing just

stage('Build') {
  steps {
    sh 'chmod +x gradlew'
    sh './gradlew build jar'
  }
}

is perfectly fine.

Gradle daemons stop themselves after being idle for 3 hours (FAQ). If a build runs just once a day the daemon will be dead for sure. This is usually the reason why the daemon is absent and needs to be started.

Gradle might also decide to start a new daemon instance if the running daemon is classified incompatible (build environment, e.g. heap memory settings, changed). This is explicitly highlighted in the build output according to my information.

With regards to slow daemon startup performance, the usual advice to run the build on the latest Gradle and Java versions.

One last tip though. Should you be using Git as version control system, you can get rid of the sh 'chmod +x gradlew' by letting Git setting the executable flag via update-index:

git update-index --chmod=+x gradlew

Upvotes: 1

Related Questions