Ido Ran
Ido Ran

Reputation: 11414

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.

Upvotes: 15

Views: 9153

Answers (1)

Christopher Orr
Christopher Orr

Reputation: 111623

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"
    }
  }
}

Upvotes: 11

Related Questions