Docker caching of .m2 doesn't work locally

2019-04-14 02:08发布

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条回答
Emotional °昔
2楼-- · 2019-04-14 03:03

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.paths value 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 /cache references above with /host-cache which doesn't depend on any undocumented runner defaults.

查看更多
登录 后发表回答