how to add persistent volume for Maven in Gitlab w

2019-07-26 22:31发布

Situation:

  • server A: we run Gitlab in a container.
  • server B: we have Kubernetes.

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

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.

Pretty basic stuff except I am absolutely new to Gitlab and Kubernetes.

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 :

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

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.

Running GitLab Community Edition 11.6.5.

2条回答
聊天终结者
2楼-- · 2019-07-26 22:41

1) Create a Kubernetes PersistentVolume (I use NFS as PersistentVolume type) :

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

2) create a Kubernetes PersistentVolumeClaim :

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

3) Refer the PersistentVolumeClaim in your config.toml :

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

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

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

variables:
  MVN_OPTS: "-Dmaven.repo.local=/cache/maven.repository"
查看更多
趁早两清
3楼-- · 2019-07-26 22:53

I would use a separate cache per project, using this in your build configuration

variables:
  MAVEN_OPTS: "-Dmaven.repo.local=./.m2/repository"
cache:
  paths:
    - ./.m2/repository
  # share cache across branches
  key: "$CI_BUILD_REF_NAME"

This prevents interference between separate project builds. You can find a references configuration from the gitlab guys: https://gitlab.com/gitlab-org/gitlab-ci-yml/blob/master/Maven.gitlab-ci.yml

查看更多
登录 后发表回答