kubernetesjenkinsjenkins-pipelinekanikojenkins-kubernetes

Kaniko Image Cache in Jenkins Kubernetes Agents


Here's the Jenkinsfile, I'm spinning up:

pipeline {
    agent {
        kubernetes {
            yaml '''
                    apiVersion: v1
                    kind: Pod
                    metadata:
                      name: kaniko
                      namespace: jenkins
                    spec:
                      containers:
                      - name: kaniko
                        image: gcr.io/kaniko-project/executor:v1.8.1-debug
                        imagePullPolicy: IfNotPresent
                        command:
                        - /busybox/cat
                        tty: true
                        volumeMounts:
                        - name: jenkins-docker-cfg
                          mountPath: /kaniko/.docker
                        - name: image-cache
                          mountPath: /cache
                      imagePullSecrets:
                      - name: regcred
                      volumes:
                      - name: image-cache
                        persistentVolumeClaim:
                          claimName: kaniko-cache-pvc
                      - name: jenkins-docker-cfg
                        projected:
                          sources:
                          - secret:
                              name: regcred
                              items:
                                - key: .dockerconfigjson
                                  path: config.json
                    ''' 
        }
    }
    stages {
        stage('Build & Cache Image'){
            steps{
                container(name: 'kaniko', shell: '/busybox/sh') {
                 withEnv(['PATH+EXTRA=/busybox']) {
                 sh '''#!/busybox/sh -xe
                    /kaniko/executor \
                    --cache \
                    --cache-dir=/cache \
                    --dockerfile Dockerfile \
                    --context `pwd`/Dockerfile \
                    --insecure \
                    --skip-tls-verify \
                    --destination testrepo/kaniko-test:0.0.1''' 
                    }       
                }
            }
        }
    }
}

Problem is the executor doesn't dump the cache anywhere I can find. If I rerun the pod and stage, the executor logs say that there's no cache. I want to retain the cache using a PVC as you can see. Any thoughts? Do I miss something?

Thanks in advance.


Solution

  • You should use separate pod kaniko-warmer, which will download you specific images.

      - name: kaniko-warmer
        image: gcr.io/kaniko-project/warmer:latest
        args: ["--cache-dir=/cache",
               "--image=nginx:1.17.1-alpine",
               "--image=node:17"]   
        volumeMounts:
          - name: kaniko-cache
            mountPath: /cache
      volumes: 
      - name: kaniko-cache
        hostPath:
          path: /opt/volumes/database/qazexam-front-cache
          type: DirectoryOrCreate 
    

    Then volume kaniko-cache could be mounted to kaniko executor