<p>I'm trying to keep my workspace in <code>PersistentVolumeClaim</code> by using <a href="https://github.com/jenkinsci/kubernetes-plugin" rel="noreferrer">kubernetes-plugin</a></p>
<p>I've created <code>PV</code> and <code>PVC</code> 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.</p>
<p>Here is my pipeline. Any Idea why don't work?</p>
<pre><code>def podTemplate = &quot;&quot;&quot;
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: maven
    image: maven
    command:
    - sleep
    args:
    - infinity
    volumeMounts:
      - name: workspace-volume
        mountPath: /home/jenkins/agent
    workingDir: &quot;/home/jenkins/agent&quot;
  volumes:
      - name: &quot;workspace-volume&quot;
        persistentVolumeClaim:
          claimName: &quot;jenkins-slave-pvc&quot;
          readOnly: false
&quot;&quot;&quot;


pipeline {
    agent none
    stages {
        stage ('maven') {
            agent { 
                kubernetes {
                    yaml podTemplate 
                    defaultContainer 'maven' 
                } 
            }
            stages {
                stage('Nested 1') {                  
                    steps {
                        sh &quot;touch Nested1 &amp;&amp; mvn -version&quot;
                    }
                }
                stage('Nested 2') {                  
                    steps {
                        sh &quot;mvn -version 2 &amp;&amp; touch Nested2 &quot;
                    }
                }
            }
        }
    }
}
</code></pre>
<p>Now Jenkins always mount the volume like this:</p>
<pre><code>volumeMounts:
 - mountPath: &quot;/home/jenkins/agent&quot;
   name: &quot;workspace-volume&quot;
   readOnly: false
volumes:
  - emptyDir:
      medium: &quot;&quot;
    name: &quot;workspace-volume&quot;
</code></pre>
<p><strong>My question is:</strong> How can I overwrite the default value of <code>emptyDir</code> with my <code>persistentVolumeClaim</code> to be my workspace-volume?</p>

<p>To help anyone who comes across this question much later like I have, there is property on <code>kubernetes</code> in the <code>agent</code> 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.</p>
<p>So to use an existing persistent volume claim, you would do:</p>
<pre><code>pipeline {
    agent {
        kubernetes {
            yaml podTemplate
            workspaceVolume persistentVolumeClaimWorkspaceVolume(claimName: 'jenkins-slave-pvc', readOnly: false)
        }
    }
...
}
</code></pre>
<p>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:</p>
<pre><code>pipeline {
    agent {
        kubernetes {
            yaml podTemplate
            workspaceVolume dynamicPVC(accessModes: 'ReadWriteOnce', requestsSize: &quot;10Gi&quot;)
        }
    }
...
}
</code></pre>
