Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cache Gradle dependencies inside Gitlab CI

I add cache property inside my gitlab-ci.yml file in my Android project.

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

But in each pipeline when I run ./gradlew assemble, It downloads all gradle dependencies which cause slow build time.

like image 949
Адриан Avatar asked Jun 26 '17 10:06

Адриан


People also ask

How do I cache Gradle dependencies?

Generally, you can refresh dependencies in your cache with the command line option --refresh-dependencies. You can also delete the cached files under ~/. gradle/caches . With the next build Gradle would attempt to download them again.

Where are Gradle dependencies cached?

The Gradle dependency cache consists of two storage types located under GRADLE_USER_HOME/caches : A file-based store of downloaded artifacts, including binaries like jars as well as raw downloaded meta-data like POM files and Ivy files.

What is cache in Gitlab CI?

all tiers. A cache is one or more files a job downloads and saves. Subsequent jobs that use the same cache don't have to download the files again, so they execute more quickly.


2 Answers

I'm doing it like this

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

cache:
  paths:
    - .gradle/wrapper
    - .gradle/caches
like image 126
Martin Gerhardy Avatar answered Sep 20 '22 13:09

Martin Gerhardy


Gitlab runner will not cache files outside of your project directory (e.g. ./gradle folder), So you need to cache all dependencies inside your project directory.

You can use -g command which specifies the gradle user home directory.

For example you can call ./gradlew -g /cache assemble to cache your dependencies in /cache folder.

like image 42
Saeed Masoumi Avatar answered Sep 19 '22 13:09

Saeed Masoumi