6

I'm trying to keep my workspace in PersistentVolumeClaim by using kubernetes-plugin

I've created PV and PVC and I've stored my files on local disk. This pipeline has worked fine before but now workspace's are not created anymore on local disk.

Here is my pipeline. Any Idea why don't work?

def podTemplate = """
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: maven
    image: maven
    command:
    - sleep
    args:
    - infinity
    volumeMounts:
      - name: workspace-volume
        mountPath: /home/jenkins/agent
    workingDir: "/home/jenkins/agent"
  volumes:
      - name: "workspace-volume"
        persistentVolumeClaim:
          claimName: "jenkins-slave-pvc"
          readOnly: false
"""


pipeline {
    agent none
    stages {
        stage ('maven') {
            agent { 
                kubernetes {
                    yaml podTemplate 
                    defaultContainer 'maven' 
                } 
            }
            stages {
                stage('Nested 1') {                  
                    steps {
                        sh "touch Nested1 && mvn -version"
                    }
                }
                stage('Nested 2') {                  
                    steps {
                        sh "mvn -version 2 && touch Nested2 "
                    }
                }
            }
        }
    }
}

Now Jenkins always mount the volume like this:

volumeMounts:
 - mountPath: "/home/jenkins/agent"
   name: "workspace-volume"
   readOnly: false
volumes:
  - emptyDir:
      medium: ""
    name: "workspace-volume"

My question is: How can I overwrite the default value of emptyDir with my persistentVolumeClaim to be my workspace-volume?

Community
  • 1
  • 1
airdata
  • 577
  • 1
  • 7
  • 23
  • Unless you really need to use Jenkins, I would consider Tekton Pipelines with good support for PVC as workspace between tasks. https://github.com/tektoncd/pipeline – Jonas May 03 '20 at 23:27
  • 1
    Tekton does not support OpenShift 3.x or Kubernetes pre 1.15. Some cannot update these. – trash80 Jan 21 '21 at 18:49

1 Answers1

4

To help anyone who comes across this question much later like I have, there is property on kubernetes in the agent section, that defines how the workspace volume is handled. Jenkins will extend whatever your agent YAML file contains with a mount and volume for the workspace.

So to use an existing persistent volume claim, you would do:

pipeline {
    agent {
        kubernetes {
            yaml podTemplate
            workspaceVolume persistentVolumeClaimWorkspaceVolume(claimName: 'jenkins-slave-pvc', readOnly: false)
        }
    }
...
}

Depending on how your persistent volume is set up and what kind of parallel builds you support, you may want to go with dynamic persistent volume claims instead, so that each build agent gets it's own workspace volume. You can configure that functionality with:

pipeline {
    agent {
        kubernetes {
            yaml podTemplate
            workspaceVolume dynamicPVC(accessModes: 'ReadWriteOnce', requestsSize: "10Gi")
        }
    }
...
}