Search code examples
mavengitlab-cigitlab-ci-runner

How to enable maven artifact caching for GitLab CI runner?


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?


Solution

  • 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:

    cache:
      paths:
        - .m2/repository
    
    variables:
      MAVEN_OPTS: "-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository"
    
    maven_job:
      script:
        - mvn clean install