gitlab ci cache no matching files

I banged my head on the same issue.

MS Berends is partially right. Caching is supposed to work for files and folders only already within your project directory, see here: https://gitlab.com/gitlab-org/gitlab-ce/issues/4431

There were supposed to be an option of mounting the cache folder as a volume like

[[runners]]
  name = ""
  url = ""
  token = ""
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "alpine:latest"
    privileged = true
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache", "/root/.gradle:/root/.gradle"]
    shm_size = 0

But that does not work neither.

What I ended up doing is the following:

  1. In my .gitlab-ci.yaml, I set the GRADLE_USER_HOME to point on the already mapped cache volume like

    GRADLE_USER_HOME: "/cache/.gradle"
    
  2. Then I passed that gradle home variable to the ./gradlew like

    ./gradlew $GRADLE_ARGS_CI -g $GRADLE_USER_HOME testDebugUnitTest
    
  3. Notice the argument named $GRADLE_ARGS_CI. It is set to the following value

    GRADLE_ARGS_CI: "--no-build-cache --no-daemon --stacktrace"
    

The --no-build-cache is necessary if you don't want to reuse the build outputs from previous builds. The --no-daemon is a no brainer because the docker build environment is spawned for every build.

I was able to save 2.5 min on my build time with these changes.


That is because cache only works for files and folders INSIDE your project. This is poorly documented on the GitLab website IMHO.

So:

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

Still only searches in:

/home/user/yourproject/root/.gradle/caches
/home/user/yourproject/root/.gradle/wrapper

For R, I set R_LIBS_SITE to a local folder inside my project. This allowed me to reuse installed packages. Have a look here.