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

4/1/2019

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.

-- dippynark
jenkins
kubernetes

0 Answers