30

I try to build apk with gitlab runner

When I build apk, I don't want download all build pacakage everytime

so i try to caching .gradle/caches and .gradle/wrappers

following is my gitlab-ci.yml

sdk_build_job
  image: myimage:latest
  stage: sdk-build
  script:
    ...
  cache:
    key: gradle-cache
      - /root/.gradle/caches
      - /root/.gradle/wrapper

but create gradle-cache always make an warning

Creating cache gradle-cache...
WARNING: /root/.gradle/caches: no matching files       
WARNING: /root/.gradle/wrapper: no matching files      
Archive is up to date!                             

I don't know why can't find caches and wrapper directory

When i into docker container and find the folders, there were well positioned

root@runner-3d9fa57b-project-4-concurrent-0:~/.gradle# pwd
/root/.gradle
root@runner-3d9fa57b-project-4-concurrent-0:~/.gradle# ls -al
total 28
drwxr-xr-x 7 root root 4096 Dec 28 02:21 .
drwx------ 1 root root 4096 Dec 28 02:19 ..
drwxr-xr-x 6 root root 4096 Dec 28 02:20 caches
drwxr-xr-x 3 root root 4096 Dec 28 02:19 daemon
drwxr-xr-x 4 root root 4096 Dec 28 02:19 native
drwxr-xr-x 2 root root 4096 Dec 28 02:21 workers
drwxr-xr-x 3 root root 4096 Dec 28 02:19 wrapper

Please help me.......

fuzes
  • 1,777
  • 4
  • 20
  • 40

3 Answers3

51

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.

MS Berends
  • 4,489
  • 1
  • 40
  • 53
  • 3
    Oh that warning was driving me crazy, and I didn't find it at GitLab docs. But it makes sense that only project files could be cached in a context of a CI Pipeline Job. Thanks!! – Guillem López Garcia Feb 15 '22 at 18:37
7

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.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Hamady C.
  • 1,205
  • 11
  • 13
-5
cache:
  paths:
    - .nuget

before_script:
  - dotnet --version
  - "[ -f .nuget/NuGet/NuGet.Config ] && rm -rf $HOME/.nuget || rsync -a $HOME/.nuget ./"
  - ln -s $(pwd)/.nuget $HOME/.nuget
mokeyish
  • 1
  • 1