问题
Consider this of .gitlab-ci.yml:
variables:
MAVEN_OPTS: "-Dmaven.repo.local=/root/.m2/repository"
cache:
key: "M2"
paths:
- /root/.m2/repository
This works correctly when running directly in gitlab, but it doesn't work locally when running as:
gitlab-runner exec docker test
If I run it locally like this, I can see messages like Successfully extracted cache, but then it re-downloads all the maven dependencies, which in my case meaning downloading hundreds of megabytes.
I cannot see the difference between running it locally and remotely, both of them should work the same I guess?
Where is this cache stored? Can I somehow verify there is actually something stored?
回答1:
As in this issue, you need to put your m2 cache folder into a volume, or the container would start from scratch (empty cache) every time, promting maven to download everything all over again.
That issue also reports, instead of using volumes:
The
cache.pathsvalue doesn't need to be specified if you point the repo to something in/cache; the version I'm actually using currently is below, and I don't see the 'Removing..' line for.m2:
image:
maven:3.3.9-jdk-8
variables:
MAVEN_OPTS: -Dmaven.repo.local=/cache/maven.repository
test:
script: "mvn test"
But using a volume remains the most portable solution:
We edit our config to include a local docker host volume named
host-cache, and we just replace/cachereferences above with/host-cachewhich doesn't depend on any undocumented runner defaults.
来源:https://stackoverflow.com/questions/48654123/docker-caching-of-m2-doesnt-work-locally