dippynark
dippynark

Reputation: 3003

How can I share multiple Docker caches between Jenkins pipeline runs?

I have a Jenkins pipeline using the kubernetes plugin to run a docker in docker container and build images:

pipeline {
  agent {
    kubernetes {
      label 'kind'
      defaultContainer 'jnlp'
      yaml """
apiVersion: v1
kind: Pod
metadata:
  labels:
    name: dind
... 

I also have a pool of persistent volumes in the jenkins namespace each labelled app=dind. I want one of these volumes to be picked for each pipeline run and used as /var/lib/docker in my dind container in order to cache any image pulls on each run. I want to have a pool and caches, not just a single one, as I want multiple pipeline runs to be able to happen at the same time. How can I configure this?

This can be achieved natively in kubernetes by creating a persistent volume claim as follows:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: dind
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  selector:
    matchLabels:
      app: dind

and mounting it into the Pod, but I'm not sure how to configure the pipeline to create and cleanup such a persistent volume claim.

Upvotes: 2

Views: 1153

Answers (1)

Dee
Dee

Reputation: 161

First of all, I think the way you think it can be achieved natively in kubernetes - wouldn't work. You either have to re-use same PVC which will make build pods to access same PV concurrently, or if you want to have a PV per build - your PVs will be stuck in Released status and not automatically available for new PVCs.

There is more details and discussion available here https://issues.jenkins.io/browse/JENKINS-42422.

It so happens that I wrote two simple controllers - automatic PV releaser (that would find and make Released PVs Available again for new PVCs) and dynamic PVC provisioner (for Jenkins Kubernetes plugin specifically - so you can define a PVC as annotation on a Pod). Check it out here https://github.com/plumber-cd/kubernetes-dynamic-reclaimable-pvc-controllers. There is a full Jenkinsfile example here https://github.com/plumber-cd/kubernetes-dynamic-reclaimable-pvc-controllers/tree/main/examples/jenkins-kubernetes-plugin-with-build-cache.

Upvotes: 2

Related Questions