Cache in Gitlab CI

When using gitlab ci one might know the cache property. With defining cached files these are used for subsequent build (instead of downloading them again). This is extremly useful when using a docker executor, the shell executor doesn’t need a cache as long as a build doesn’t remove downloaded or generated artifacts.

But the build can only cache artifacts in the current working directory, so you can’t cache e.g. the local .m2 folder in order to avoid downloading artifacts every build. Furthermore everything inside the /cache folder is automatically cached by the ci runner.

Therefore if we could make maven download its artifacts to /cache/.m2 instead of ${USER_HOME}/.m2 all downloaded artifacts will be cached. This can be done by providing -Dmaven.repo.local parameter:

image: java
stages:
  - build
mvn-package:
  stage: build
  script: "./mvnw package -Dmaven.repo.local=/cache"

When executing the build a second time it doesn’t download all artifacts anymore, only changed ones. For me as a huge gradle fan I needed cache for gradle dependencies as well. This is also very easy and even caches the downloaed gradle wrapper for subsequent builds:

image: java
stages:
  - build
gradle-assemble:
  stage: build
  script: "./gradlew -g /cache assemble"
Frederik Hahne

Frederik Hahne

father, @jhipster board member, developing digital change @wescalehq, @jugpaderborn leader, geek and nerd

Read More
Cache in Gitlab CI
Share this