Run time create pv/pvc for each jenkins jobs running on slave agent

4/30/2020

I am looking to create pv/pvc for each jenkins jobs running on slave agent on runtime. Basically what I am trying to achieve is, create a pv and share it between pods and later delete it when job is done.

pipeline {
    agent {
        kubernetes {
            label 'scm'
            yaml """
              apiVersion: storage.k8s.io/v1
              kind: StorageClass
              metadata:
                name: fast
              provisioner: kubernetes.io/gce-pd
              parameters:
                type: pd-ssd
              ---
              apiVersion: v1
              kind: PersistentVolumeClaim
              metadata:
                name: claim1
              spec:
                accessModes:
                  - ReadWriteMany
                storageClassName: fast
                resources:
                  requests:
                    storage: 1Gi
              ---
              apiVersion: "v1"
              kind: "Pod"
              spec:
                containers:
                  image: "jenkins/jnlp-slave:3.35-5-alpine"
                  name: "jnlp"
                  volumeMounts:
                  - mountPath: "/home/jenkins/agent"
                    name: "workspace-volume"
                    readOnly: false
                - command:
                  - "cat"
                  tty: true
                  volumeMounts:
                  - mountPath: "/home/jenkins/wsp1"
                    name: "workspace-volume"
                    readOnly: false
                volumes:
                  - name: "workspace-volume"
                    persistentVolumeClaim:
                      claimName: claim1


              """
            }
        }
    stages {
        stage('Checkout code') {
            agent { label 'scm'}
            steps {
                git branch: 'master',
                credentialsId: 'key',
                url: 'giturl'

                sh "ls -lat"
            }
        }
        stage('Build ') {
            agent {
              kubernetes {
                  label 'Build-pod'
                  yaml """
                    spec:
                      containers:
                      - name: maven
                        image: maven:3.3.9-jdk-8-alpine
                        command:
                        - cat
                        tty: true
                      volumeMounts:
                      - mountPath: "/home/jenkins/agent"
                        name: "workspace-volume"
                        readOnly: false
                    """
              }
            }
            steps {

                sh "echo Workspace dir is ${pwd()}"
                sh "mvn  clean install  
            }
        }
    }

}

Above script does not work for obvious reasons. Do we have any other solutions. and how to use runtime name for pvc and use it pod.

metadata:
 name: claim1

I am running jenkins helm on k8s.

-- pythonhmmm
helm-jenkins
jenkins
jenkins-pipeline
kubernetes

1 Answer

5/12/2020

You can create a PVC per pod using something like workspaceVolume: dynamicPVC(requestsSize: "10Gi") but it will be tied to the pod and deleted when the pod is deleted

https://github.com/jenkinsci/kubernetes-plugin/blob/342166c1864e84791f2e94dd823709eb6e672a6e/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/dynamicPVC.groovy

-- csanchez
Source: StackOverflow