如何在使用Docker运行Jenkins时配置Gradle缓存

时间:2018-01-02 21:23:59

标签: jenkins gradle jenkins-pipeline

我正在建立Jenkins管道,用于使用Gradle构建项目 詹金斯有几个奴隶。所有从站都连接到NAS 一些构建步骤在Docker容器中运行Gradle,而其他构建步骤直接在从属服务器上运行。

目标是尽可能多地使用缓存,但我也遇到了诸如

之类的死锁问题
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.

1 个答案:

答案 0 :(得分:3)

由于上面评论中提到的Gradle问题,我做了类似的事情 - 在启动时将Gradle缓存复制到容器中,并在构建结束时写回任何更改:

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