Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Gradle cache when running Jenkins with Docker

I'm working on building Jenkins pipeline for building a project with Gradle.
Jenkins has several slaves. All the slaves are connected to a NAS.
Some of the build steps run Gradle inside Docker containers while others run directly on the slaves.

The goal is to use as much cache as possible but I have also run into deadlock issues such as

Could not create service of type FileHasher using GradleUserHomeScopeServices.createCachingFileHasher().
> Timeout waiting to lock file hash cache (/home/slave/.gradle/caches/4.2/fileHashes). It is currently in use by another Gradle instance.
like image 849
Ido Ran Avatar asked Jan 02 '18 21:01

Ido Ran


People also ask

Does Jenkins cache Docker images?

Still the bottom line is: Jenkins does not cache automagically for you. Caching is inside the scope of the build tool(s) that you are using. You have to take care to incorporate that properly to your CI environment's needs. But of course it is possible to achieve.

How do you pass parameters from Jenkins to Gradle?

There you have to use “-P”. Within the Jenkins configuration page in section “Invoke Gradle script” there is the “Switches” textbox. Here you define “-PParam1=${VALUE) -PParam2=${VALUE_2}” (in case these VALUES are know to Jenkins).


1 Answers

Due to the Gradle issue mentioned in the comment above, I do something like this — copying the Gradle cache into the container at startup, and writing any changes back at the end of the build:

pipeline {
  agent {
    docker {
      image '…'
      // Mount the Gradle cache in the container
      args  '-v /var/cache/gradle:/tmp/gradle-user-home:rw'
    }
  }
  environment {
    HOME = '/home/android'
    GRADLE_CACHE = '/tmp/gradle-user-home'
  }
  stages {
    stage('Prepare container') {
      steps {
        // Copy the Gradle cache from the host, so we can write to it
        sh "rsync -a --include /caches --include /wrapper --exclude '/*' ${GRADLE_CACHE}/ ${HOME}/.gradle || true"
      }
    }
    …
  }
  post {
    success {
      // Write updates to the Gradle cache back to the host
      sh "rsync -au ${HOME}/.gradle/caches ${HOME}/.gradle/wrapper ${GRADLE_CACHE}/ || true"
    }
  }
}
like image 100
Christopher Orr Avatar answered Sep 19 '22 15:09

Christopher Orr