<p>Situation: </p>

<ul>
<li>server A: we run Gitlab in a container. </li>
<li>server B: we have Kubernetes.</li>
</ul>

<p>Gitlab uses Kubernetes runner. Some of our projects then build applications using docker container with Git and Maven.</p>

<p>Maven always has to download all kinds of things into it's /root/.m2 cache.
What I need to do is create a persistent volume that these jobs can use, so once it's downloaded, it doesn't have to do it again each time someone wants to build or test something. These containers are always built anew using one premade image.</p>

<p>Pretty basic stuff except I am absolutely new to Gitlab and Kubernetes.</p>

<p>Where do I need to create the volume? I tried to change config.toml in the runner to include host_path type volume, but I don't know if I succeeded and Maven certainly has to download all the requirements every time. I don't even know if the runner container has to be restarted for the changes to be applicated, and how.
This is the runner's config.toml :</p>

<pre><code>listen_address = "[::]:9252"
concurrent = 4
check_interval = 3
log_level = "info"

[session_server]
  session_timeout = 1800

[[runners]]
  name = "runner-gitlab-runner-c55d9bf98-2nn7c"
  url = "https://private_network:8443/"
  token = "yeah, token"
  executor = "kubernetes"
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
  [runners.kubernetes]
    host = ""
    bearer_token_overwrite_allowed = false
    image = "ubuntu:16.04"
    namespace = "gitlab-managed-apps"
    namespace_overwrite_allowed = ""
    privileged = true
    service_account_overwrite_allowed = ""
    pod_annotations_overwrite_allowed = ""
    [runners.kubernetes.volumes.host_path]
      name = "maven-volume"
      mount_path = "/root/.m2"
      read_only = false
</code></pre>

<p>I don't know enough to know what I am missing. Maybe I have to define something in .gitlab-ci.yml in those projects, or something else. I have looked into tutorials, I have tried Gitlab help pages, but I still can't find a working solution.</p>

<p>Running GitLab Community Edition 11.6.5.</p>

Petr
<p>1) Create a Kubernetes PersistentVolume (I use NFS as PersistentVolume type) :</p>

<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: gitlabrunner-nfs-volume
spec:
  accessModes:
  - ReadWriteMany
  capacity:
    storage: 15Gi
  mountOptions:
  - nolock
  nfs:
    path: /kubernetes/maven/
    server: NFS_SERVER_IP
  persistentVolumeReclaimPolicy: Recycle
</code></pre>

<p>2) create a Kubernetes PersistentVolumeClaim : </p>

<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: gitlabrunner-claim
  namespace: gitlab
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 15Gi
  volumeName: gitlabrunner-nfs-volume
status:
  accessModes:
  - ReadWriteMany
  capacity:
    storage: 15Gi
</code></pre>

<p>3) Refer the PersistentVolumeClaim in your config.toml :</p>

<pre><code>   [[runners.kubernetes.volumes.pvc]]
     mount_path = "/cache/maven.repository"
     name = "gitlabrunner-claim"
</code></pre>

<p>This enables to mount the volume each time a container is launched with this configuration. </p>

<p>4) in .gitlab-ci.yml file, set the MVN_OPTS like answered by @thomas :</p>

<pre><code>variables:
  MVN_OPTS: "-Dmaven.repo.local=/cache/maven.repository"
</code></pre>
