Gitlab CI gradle依赖缓存

Posted

技术标签:

【中文标题】Gitlab CI gradle依赖缓存【英文标题】:Gitlab CI gradle dependency cache 【发布时间】:2016-03-13 17:47:00 【问题描述】:

我在 Gitlab CI 上构建 android 并且每次下载依赖项都很烦人。我试过缓存:

$HOME/.gradle/
$HOME/.gradle/caches/
$GRADLE_HOME/caches/
$HOME/.m2/
.gradle/
build/
app/build/

我的 .gitlab-ci.yml 的第 26594 次迭代是 here。我已经在我的构建环境中安装了 gradle,因为没有缓存 gradlew 每次都会下载它,这需要更多时间。我用于构建的图像是here。我的 CI 服务器启动脚本可以在here 找到。我假设,如果缓存有效,我不会在每个构建中看到我的依赖项列表。

构建日志:

gitlab-ci-multi-runner 0.7.2 (998cf5d) 使用带有图像 android 的 Docker 执行器... 拉取 docker 镜像 gitlab/gitlab-runner:build ... 拉取 docker 镜像 android:latest ... 警告:无法提取最新版本的图像 android:latest:错误:找不到图像库/android:latest 警告:将使用本地找到的图像。

Running on runner-76acd9d3-project-74192-concurrent-0 via ff9200d4be41...
Fetching changes...
Removing .gradle/
Removing artifacts.upload.log
Removing rxfirebaseandroid/build/
HEAD is now at 7f151df djgfj
Checking out 7f151df2 as ci-gitlab...
HEAD is now at 7f151df... djgfj
Restoring cache...

$ gradle testDebug
Download https://jcenter.bintray.com/com/android/tools/build/gradle/1.3.0/gradle-1.3.0.pom
Download https://jcenter.bintray.com/com/android/tools/build/gradle-core/1.3.0/gradle-core-1.3.0.pom
Download https://jcenter.bintray.com/com/android/tools/build/builder/1.3.0/builder-1.3.0.pom
Download https://jcenter.bintray.com/com/android/tools/lint/lint/24.3.0/lint-24.3.0.pom
Download https://jcenter.bintray.com/net/sf/proguard/proguard-gradle/5.2.1/proguard-gradle-5.2.1.pom
Download https://jcenter.bintray.com/net/sf/proguard/proguard-parent/5.2.1/proguard-parent-5.2.1.pom
Download https://jcenter.bintray.com/com/android/tools/build/builder-model/1.3.0/builder-model-1.3.0.pom
Download https://jcenter.bintray.com/com/android/tools/build/builder-test-api/1.3.0/builder-test-api-1.3.0.pom
Download https://jcenter.bintray.com/com/android/tools/sdklib/24.3.0/sdklib-24.3.0.pom
Download https://jcenter.bintray.com/com/android/tools/sdk-common/24.3.0/sdk-common-24.3.0.pom
Download https://jcenter.bintray.com/com/android/tools/common/24.3.0/common-24.3.0.pom
Download https://jcenter.bintray.com/com/android/tools/build/manifest-merger/24.3.0/manifest-merger-24.3.0.pom
Download https://jcenter.bintray.com/com/android/tools/ddms/ddmlib/24.3.0/ddmlib-24.3.0.pom
Download https://jcenter.bintray.com/com/squareup/javawriter/2.5.0/javawriter-2.5.0.pom
Download https://jcenter.bintray.com/org/sonatype/oss/oss-parent/7/oss-parent-7.pom
Download https://jcenter.bintray.com/org/bouncycastle/bcpkix-jdk15on/1.48/bcpkix-jdk15on-1.48.pom
Download https://jcenter.bintray.com/org/bouncycastle/bcprov-jdk15on/1.48/bcprov-jdk15on-1.48.pom
Download https://jcenter.bintray.com/org/ow2/asm/asm/5.0.3/asm-5.0.3.pom
Download https://jcenter.bintray.com/org/ow2/asm/asm-parent/5.0.3/asm-parent-5.0.3.pom
Download https://jcenter.bintray.com/org/ow2/ow2/1.3/ow2-1.3.pom
Download https://jcenter.bintray.com/org/ow2/asm/asm-tree/5.0.3/asm-tree-5.0.3.pom
Download https://jcenter.bintray.com/com/android/tools/jack/jack-api/0.9.0/jack-api-0.9.0.pom
Download https://jcenter.bintray.com/com/android/tools/jill/jill-api/0.9.0/jill-api-0.9.0.pom
Download https://jcenter.bintray.com/com/android/tools/lint/lint-checks/24.3.0/lint-checks-24.3.0.pom
Download https://jcenter.bintray.com/org/eclipse/jdt/core/compiler/ecj/4.4.2/ecj-4.4.2.pom
Download https://jcenter.bintray.com/net/sf/proguard/proguard-base/5.2.1/proguard-base-5.2.1.pom
Download https://jcenter.bintray.com/com/android/tools/annotations/24.3.0/annotations-24.3.0.pom
Download https://jcenter.bintray.com/com/android/tools/layoutlib/layoutlib-api/24.3.0/layoutlib-api-24.3.0.pom
Download https://jcenter.bintray.com/com/android/tools/dvlib/24.3.0/dvlib-24.3.0.pom
Download https://jcenter.bintray.com/com/google/code/gson/gson/2.2.4/gson-2.2.4.pom
Download https://jcenter.bintray.com/org/apache/commons/commons-compress/1.8.1/commons-compress-1.8.1.pom
Download https://jcenter.bintray.com/org/apache/commons/commons-parent/33/commons-parent-33.pom
Download https://jcenter.bintray.com/org/apache/apache/13/apache-13.pom
Download https://jcenter.bintray.com/org/apache/httpcomponents/httpclient/4.1.1/httpclient-4.1.1.pom
Download https://jcenter.bintray.com/org/apache/httpcomponents/httpcomponents-client/4.1.1/httpcomponents-client-4.1.1.pom
Download https://jcenter.bintray.com/org/apache/httpcomponents/project/4.1.1/project-4.1.1.pom
Download https://jcenter.bintray.com/org/apache/httpcomponents/httpmime/4.1/httpmime-4.1.pom
Download https://jcenter.bintray.com/org/apache/httpcomponents/httpcomponents-client/4.1/httpcomponents-client-4.1.pom
Download https://jcenter.bintray.com/com/google/guava/guava/17.0/guava-17.0.pom
Download https://jcenter.bintray.com/com/google/guava/guava-parent/17.0/guava-parent-17.0.pom
Download https://jcenter.bintray.com/net/sf/kxml/kxml2/2.3.0/kxml2-2.3.0.pom
Download https://jcenter.bintray.com/com/android/tools/lint/lint-api/24.3.0/lint-api-24.3.0.pom
Download https://jcenter.bintray.com/org/ow2/asm/asm-analysis/5.0.3/asm-analysis-5.0.3.pom
Download https://jcenter.bintray.com/com/intellij/annotations/12.0/annotations-12.0.pom
Download https://jcenter.bintray.com/org/apache/httpcomponents/httpcore/4.1/httpcore-4.1.pom
Download https://jcenter.bintray.com/org/apache/httpcomponents/httpcomponents-core/4.1/httpcomponents-core-4.1.pom
Download https://jcenter.bintray.com/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.pom
Download https://jcenter.bintray.com/org/apache/commons/commons-parent/5/commons-parent-5.pom
Download https://jcenter.bintray.com/org/apache/apache/4/apache-4.pom
Download https://jcenter.bintray.com/commons-codec/commons-codec/1.4/commons-codec-1.4.pom
Download https://jcenter.bintray.com/org/apache/commons/commons-parent/11/commons-parent-11.pom
Download https://jcenter.bintray.com/com/android/tools/external/lombok/lombok-ast/0.2.3/lombok-ast-0.2.3.pom
Download https://jcenter.bintray.com/com/android/tools/build/gradle/1.3.0/gradle-1.3.0.jar
Download https://jcenter.bintray.com/com/android/tools/build/gradle-core/1.3.0/gradle-core-1.3.0.jar
Download https://jcenter.bintray.com/com/android/tools/build/builder/1.3.0/builder-1.3.0.jar
Download https://jcenter.bintray.com/com/android/tools/lint/lint/24.3.0/lint-24.3.0.jar
Download https://jcenter.bintray.com/net/sf/proguard/proguard-gradle/5.2.1/proguard-gradle-5.2.1.jar
Download https://jcenter.bintray.com/com/android/tools/build/builder-model/1.3.0/builder-model-1.3.0.jar
Download https://jcenter.bintray.com/com/android/tools/build/builder-test-api/1.3.0/builder-test-api-1.3.0.jar
Download https://jcenter.bintray.com/com/android/tools/sdklib/24.3.0/sdklib-24.3.0.jar
Download https://jcenter.bintray.com/com/android/tools/sdk-common/24.3.0/sdk-common-24.3.0.jar
Download https://jcenter.bintray.com/com/android/tools/common/24.3.0/common-24.3.0.jar
Download https://jcenter.bintray.com/com/android/tools/build/manifest-merger/24.3.0/manifest-merger-24.3.0.jar
Download https://jcenter.bintray.com/com/android/tools/ddms/ddmlib/24.3.0/ddmlib-24.3.0.jar
Download https://jcenter.bintray.com/com/squareup/javawriter/2.5.0/javawriter-2.5.0.jar
Download https://jcenter.bintray.com/org/bouncycastle/bcpkix-jdk15on/1.48/bcpkix-jdk15on-1.48.jar
Download https://jcenter.bintray.com/org/bouncycastle/bcprov-jdk15on/1.48/bcprov-jdk15on-1.48.jar
Download https://jcenter.bintray.com/org/ow2/asm/asm/5.0.3/asm-5.0.3.jar
Download https://jcenter.bintray.com/org/ow2/asm/asm-tree/5.0.3/asm-tree-5.0.3.jar
Download https://jcenter.bintray.com/com/android/tools/jack/jack-api/0.9.0/jack-api-0.9.0.jar
Download https://jcenter.bintray.com/com/android/tools/jill/jill-api/0.9.0/jill-api-0.9.0.jar
Download https://jcenter.bintray.com/com/android/tools/lint/lint-checks/24.3.0/lint-checks-24.3.0.jar
Download https://jcenter.bintray.com/org/eclipse/jdt/core/compiler/ecj/4.4.2/ecj-4.4.2.jar
Download https://jcenter.bintray.com/net/sf/proguard/proguard-base/5.2.1/proguard-base-5.2.1.jar
Download https://jcenter.bintray.com/com/android/tools/annotations/24.3.0/annotations-24.3.0.jar
Download https://jcenter.bintray.com/com/android/tools/layoutlib/layoutlib-api/24.3.0/layoutlib-api-24.3.0.jar
Download https://jcenter.bintray.com/com/android/tools/dvlib/24.3.0/dvlib-24.3.0.jar
Download https://jcenter.bintray.com/com/google/code/gson/gson/2.2.4/gson-2.2.4.jar
Download https://jcenter.bintray.com/org/apache/commons/commons-compress/1.8.1/commons-compress-1.8.1.jar
Download https://jcenter.bintray.com/org/apache/httpcomponents/httpclient/4.1.1/httpclient-4.1.1.jar
Download https://jcenter.bintray.com/org/apache/httpcomponents/httpmime/4.1/httpmime-4.1.jar
Download https://jcenter.bintray.com/com/google/guava/guava/17.0/guava-17.0.jar
Download https://jcenter.bintray.com/net/sf/kxml/kxml2/2.3.0/kxml2-2.3.0.jar
Download https://jcenter.bintray.com/com/android/tools/lint/lint-api/24.3.0/lint-api-24.3.0.jar
Download https://jcenter.bintray.com/org/ow2/asm/asm-analysis/5.0.3/asm-analysis-5.0.3.jar
Download https://jcenter.bintray.com/com/intellij/annotations/12.0/annotations-12.0.jar
Download https://jcenter.bintray.com/org/apache/httpcomponents/httpcore/4.1/httpcore-4.1.jar
Download https://jcenter.bintray.com/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar
Download https://jcenter.bintray.com/commons-codec/commons-codec/1.4/commons-codec-1.4.jar
Download https://jcenter.bintray.com/com/android/tools/external/lombok/lombok-ast/0.2.3/lombok-ast-0.2.3.jar
Download https://jcenter.bintray.com/com/firebase/firebase-client-android/2.3.1/firebase-client-android-2.3.1.pom
Download https://jcenter.bintray.com/io/reactivex/rxjava/1.0.14/rxjava-1.0.14.pom
Download https://jcenter.bintray.com/com/firebase/firebase-client-jvm/2.3.1/firebase-client-jvm-2.3.1.pom
Download https://jcenter.bintray.com/com/fasterxml/jackson/core/jackson-databind/2.2.2/jackson-databind-2.2.2.pom
Download https://jcenter.bintray.com/com/fasterxml/oss-parent/10/oss-parent-10.pom
Download https://jcenter.bintray.com/com/firebase/tubesock/0.0.11/tubesock-0.0.11.pom
Download https://jcenter.bintray.com/com/fasterxml/jackson/core/jackson-annotations/2.2.2/jackson-annotations-2.2.2.pom
Download https://jcenter.bintray.com/com/fasterxml/jackson/core/jackson-core/2.2.2/jackson-core-2.2.2.pom
Download https://jcenter.bintray.com/com/fasterxml/jackson/core/jackson-annotations/2.2.2/jackson-annotations-2.2.2.jar
Download https://jcenter.bintray.com/com/fasterxml/jackson/core/jackson-core/2.2.2/jackson-core-2.2.2.jar
Download https://jcenter.bintray.com/com/fasterxml/jackson/core/jackson-databind/2.2.2/jackson-databind-2.2.2.jar
Download https://jcenter.bintray.com/com/firebase/tubesock/0.0.11/tubesock-0.0.11.jar
Download https://jcenter.bintray.com/com/firebase/firebase-client-jvm/2.3.1/firebase-client-jvm-2.3.1.jar
Download https://jcenter.bintray.com/com/firebase/firebase-client-android/2.3.1/firebase-client-android-2.3.1.jar
Download https://jcenter.bintray.com/io/reactivex/rxjava/1.0.14/rxjava-1.0.14.jar
Download https://jcenter.bintray.com/junit/junit/4.12/junit-4.12.pom
Download https://jcenter.bintray.com/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.pom
Download https://jcenter.bintray.com/org/hamcrest/hamcrest-parent/1.3/hamcrest-parent-1.3.pom
Download https://jcenter.bintray.com/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
Download https://jcenter.bintray.com/junit/junit/4.12/junit-4.12.jar
:rxfirebaseandroid:preBuild UP-TO-DATE
:rxfirebaseandroid:preDebugBuild UP-TO-DATE
:rxfirebaseandroid:checkDebugManifest
:rxfirebaseandroid:prepareDebugDependencies
:rxfirebaseandroid:compileDebugAidl UP-TO-DATE
:rxfirebaseandroid:compileDebugRenderscript UP-TO-DATE
:rxfirebaseandroid:generateDebugBuildConfig UP-TO-DATE
:rxfirebaseandroid:generateDebugAssets UP-TO-DATE
:rxfirebaseandroid:mergeDebugAssets UP-TO-DATE
:rxfirebaseandroid:generateDebugResValues UP-TO-DATE
:rxfirebaseandroid:generateDebugResources UP-TO-DATE
:rxfirebaseandroid:packageDebugResources UP-TO-DATE
:rxfirebaseandroid:processDebugManifest UP-TO-DATE
:rxfirebaseandroid:processDebugResources UP-TO-DATE
:rxfirebaseandroid:generateDebugSources UP-TO-DATE
:rxfirebaseandroid:processDebugJavaRes UP-TO-DATE
:rxfirebaseandroid:compileDebugJavaWithJavac UP-TO-DATE
:rxfirebaseandroid:preDebugUnitTestBuild UP-TO-DATE
:rxfirebaseandroid:prepareDebugUnitTestDependencies
:rxfirebaseandroid:processDebugUnitTestJavaRes UP-TO-DATE
:rxfirebaseandroid:compileDebugUnitTestJavaWithJavac UP-TO-DATE
:rxfirebaseandroid:compileDebugUnitTestSources UP-TO-DATE
:rxfirebaseandroid:mockableAndroidJar UP-TO-DATE
:rxfirebaseandroid:assembleDebugUnitTest UP-TO-DATE
:rxfirebaseandroid:testDebugUnitTest UP-TO-DATE

BUILD SUCCESSFUL

Total time: 51.086 secs
$ gradle assembleDebug
:rxfirebaseandroid:compileLint
:rxfirebaseandroid:copyDebugLint UP-TO-DATE
:rxfirebaseandroid:preBuild UP-TO-DATE
:rxfirebaseandroid:preDebugBuild UP-TO-DATE
:rxfirebaseandroid:checkDebugManifest
:rxfirebaseandroid:prepareDebugDependencies
:rxfirebaseandroid:compileDebugAidl UP-TO-DATE
:rxfirebaseandroid:compileDebugRenderscript UP-TO-DATE
:rxfirebaseandroid:generateDebugBuildConfig UP-TO-DATE
:rxfirebaseandroid:generateDebugAssets UP-TO-DATE
:rxfirebaseandroid:mergeDebugAssets UP-TO-DATE
:rxfirebaseandroid:generateDebugResValues UP-TO-DATE
:rxfirebaseandroid:generateDebugResources UP-TO-DATE
:rxfirebaseandroid:packageDebugResources UP-TO-DATE
:rxfirebaseandroid:processDebugManifest UP-TO-DATE
:rxfirebaseandroid:processDebugResources UP-TO-DATE
:rxfirebaseandroid:generateDebugSources UP-TO-DATE
:rxfirebaseandroid:processDebugJavaRes UP-TO-DATE
:rxfirebaseandroid:compileDebugJavaWithJavac UP-TO-DATE
:rxfirebaseandroid:extractDebugAnnotations UP-TO-DATE
:rxfirebaseandroid:mergeDebugProguardFiles UP-TO-DATE
:rxfirebaseandroid:packageDebugJar UP-TO-DATE
:rxfirebaseandroid:compileDebugNdk UP-TO-DATE
:rxfirebaseandroid:packageDebugJniLibs UP-TO-DATE
:rxfirebaseandroid:packageDebugLocalJar UP-TO-DATE
:rxfirebaseandroid:packageDebugRenderscript UP-TO-DATE
:rxfirebaseandroid:bundleDebug UP-TO-DATE
:rxfirebaseandroid:compileDebugSources UP-TO-DATE
:rxfirebaseandroid:assembleDebug UP-TO-DATE

BUILD SUCCESSFUL

Total time: 10.146 secs

Archiving cache...
INFO[0000] Creating archive cache.tgz ...               
INFO[0002] Done!                                        
Archiving artifacts...
INFO[0000] Creating archive artifacts.tgz ...           
INFO[0000] Done!                                        
Uploading artifacts...
20.0K   artifacts.tgz

Build succeeded.

【问题讨论】:

我不明白这个问题。你已经为你的 Android 测试设置了 Gitlab CI,它运行良好。虽然,你想在测试期间运行安装在你的 Gitlab 服务器上的 Gradle 和 Android SDK,而不是下载,只使用现有的,对吗?当您尝试缓存依赖项时,您能否从失败的构建中添加日志? 添加了上次构建的日志 我将构建配置为使用这些。工件工作正常。 您的问题是:您不想每次都下载依赖项,而是想从本地缓存中使用它们? 我很困惑这个问题遇到了如此多的困惑。每次提交都一遍又一遍地下载相同的 2GB 有点疯狂,那么如何缓存呢?我来回破解-v /srv/gitlab-runner/cache/:/cache/:rwcp -ru 之类的东西,但我觉得错过了一些东西。我不能告诉我的 gitlab-ci-multi-runner 以某种方式使用缓存吗? 【参考方案1】:

https://***.com/a/35478988/2026105

原来另一个帖子有这个问题的答案!有一种解决方法是运行 before_scriptGRADLE_HOME 设置为您的项目目录,然后您可以使用缓存指令进行设置。

before_script:
    - export GRADLE_USER_HOME=`pwd`/.gradle

cache:
  paths:
     - .gradle/wrapper
     - .gradle/caches

build:
  stage: build
  script:
     - ./gradlew assemble

test:
  stage: test
  script:
     - ./gradlew check

【讨论】:

太好了,这行得通。你只需要- chmod +x ./gradlew in before_script export GRADLE_USER_HOME=/cache/.gradle 这样的事情不是更好的解决方案吗?因为它总是自动缓存。 @Hubbitus - 可能 - 如果可行,可以将其添加到答案中作为替代/更简单的方法。 据我所知,如果您使用Shared Runners,这将不起作用,因为每个阶段都可能在不同的阶段运行。 @madlymad - 我认为你是对的。您需要为此使用工件,以便将文件压缩并提供给其他阶段。【参考方案2】:

根据Kamil Trzciński

我们在构建容器之外运行缓存。所以你可以缓存 绝对路径,但由于只有 /builds 和 /cache 被传输给你 无法缓存容器本地文件。

这意味着您需要将 gradle 文件放入例如/cache 文件夹

before_script:
  - export GRADLE_USER_HOME=cache/.gradle

...

cache:
  key: "$CI_COMMIT_REF_NAME" #optional: per branch caching, can be omitted
  paths:
    - $GRADLE_USER_HOME/caches/
    - $GRADLE_USER_HOME/wrapper/
    - $GRADLE_USER_HOME/build-cache/

/cache 文件夹将自动保留,但为了清晰起见,我保留了缓存配置。

【讨论】:

GRADLE_USER_HOME 和您指定的 paths 似乎不匹配? @jitter 和 PerLundberg 我不确定你们到底是什么意思。如果您有改进建议,我欢迎您对您认为不正确的地方、原因以及如何修复它进行编辑或明确解释 我很好奇,你为什么需要在你的 .gitlab-ci.yaml 中添加缓存定义,因为无论如何都会挂载 /cache 目录?另外,GitLab 提供的缓存仅适用于构建目录中的文件。

以上是关于Gitlab CI gradle依赖缓存的主要内容,如果未能解决你的问题,请参考以下文章

Gitlab CI 中缓存/工件的正确用法是啥?

在 Teamcity CI 代理中重用 Gradle 缓存

基于gitlab-ci实现maven及docker缓存的配置

【Gitlab ci】 缓存

Gitlab CI:如何从预建图像缓存 node_modules?

为啥 GitLab Ci 找不到我的缓存文件夹?