We use gitlab ci with shared runners to do our continuous integration. For each build, the runner downloads tons of maven artifacts.
Is there a way to configure gitlab ci to cache those artifacts so we can speed up the building process by preventing downloading the same artifact over and over again?
According to the conversation over on GitLab's issue tracker, I managed to change the Maven local repository path and put it into
./.m2/repository/
directory, which is we will then persist between runs by adding this global block to the CI config:Unfortunately, according to this StackOverflow answer the maven local repository path can only be set on every run with
-Dmaven.repo.local
or by editing yoursettings.xml
, which is a tedious task to do in a gitlab-ci configuration script. An option would be to set a variable with the default Maven options and pass it to every run.Also, it is crucial that the local Maven repository is a child of the current directory. For some reason, putting it in
/cache
or/builds
didn't work for me, even though someone from GitLab claimed it should.Example of a working
gitlab-ci.yml
configuration file for Maven + Java:The accepted answer didn't do it for me.
As zlobster mentioned, the guys at GitLab have this amazing repository where you can find a proper example of the
.gitlab-ci.yml
file used for Maven projects.Basically, what you need are these lines:
Keep in mind that if you decide to a add a local cache for a certain job, the global one added above will be replaced. More on this here.
You can add cache folder to gitlab-ci runner configuration and pass it to maven.
/etc/gitlab-runner/config.toml
.gitlab-ci.yml
If you are using kubernetes as executor for gitlab-runner, you can also use the maven cache. I chose to have a persistent cache on NFS with k8s PV (but other volume type are supported by gitlab-runner). The following configuration doesn't use the cache gitlab feature because of persistence offered by NFS.
1) create a PersistentVolume on your cluster, ex here with NFS (adapt to your persistence layer and your options) :
2) Reference the PV to get a claim as a volume in the runner pod :
Note (03/09/18) : A command line option for these paramaters doesn't exist yet. There is a open issue.
3) Specify the same path for the gitlab-runner cache :
or
--cache-dir "/path/to/mount/point1"
in interactive mode4) use the "/path/to/mount/point1" directory in the
-Dmaven.repo.local
optionThere is another approach. Do not use gitlab cache and use custom (per project) docker image.
Some details:
First of all, you need to create a maven docker image where all (or most of) required for your project dependencies are presented. Publish it to your registry (gitlab has one) and use it for any job running maven.
To create such an image I usually create an additional job in CI triggered manually. You need to trigger it at initial stage and when project dependencies are heavily modified.
Working sample can be found here:
https://gitlab.com/alexej.vlasov/syncer/blob/master/.gitlab-ci.yml - this project is using the prepared image and also it has a job to prepare this image.
https://gitlab.com/alexej.vlasov/maven/blob/master/Dockerfile - dockerfile to run maven and download dependencies once.
The pros:
Gitlab CI allows you to define certain paths, which contain data that should be cached between builds, on a per job or build basis (see here for more details). In combination with khmarbaise's recommendation, this can be used to cache dependencies between multiple builds.
An example that caches all job dependencies in your build: