14

I am trying to setup declarative pipeline where I would like to persiste workspace as volume claim so large git checkout can be faster. Based on doc there are options workspaceVolume and persistentVolumeClaimWorkspaceVolume but I am not able to make it work - jenkins always does following:

volumeMounts:
 - mountPath: "/home/jenkins/agent"
   name: "workspace-volume"
   readOnly: false
volumes:
  - emptyDir: {}
    name: "workspace-volume"
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Robert Ohajda
  • 141
  • 2
  • 3
  • looks like support for this was added less than 2 months ago: https://github.com/jenkinsci/kubernetes-plugin/commit/eba129aedc63a0b73039c0eaf9116251977f757a – Tavin Apr 29 '20 at 10:50
  • @yusuftezel the HiteshDhruna answer works for scripted pipeline, but this question is for declarative pipeline, so it's not answered yet. – Segfault May 01 '20 at 14:31
  • You would do well to also look into a shallow clone to minimize what gets checked out. – C.J. Feb 21 '23 at 19:41

4 Answers4

6

Try something like

podTemplate(
    containers: [
        containerTemplate(name: 'tree', image: 'iankoulski/tree', ttyEnabled: true, command: 'cat')
    ], 
    workspaceVolume: persistentVolumeClaimWorkspaceVolume(claimName: 'workspace', readOnly: false),
) {
    node(POD_LABEL) {
        stage('read workspace') {
            checkout scm
            container('tree') {
                sh 'env'
                sh 'tree'
                sh 'test -f old-env.txt && cat old-env.txt'
                sh 'env > old-env.txt'
            }
        }
    }
}
Segfault
  • 8,036
  • 3
  • 35
  • 54
hdhruna
  • 865
  • 6
  • 15
  • 1
    Can you provide a declarative pipeline example? And can you please explain whats going on here, and pare the example down to the critical parts for the persistentVolumeClaimWorkspaceVolume? For example the imagePullSecrets aren't relevant. And we can remove the logic that checks for an empty 'workspaceClaimName' and just assume it's provided right? – Segfault Apr 30 '20 at 16:21
1

Here is an example for declarative pipeline:

pipeline {
agent {
    kubernetes {
        yamlFile 'jenkins/pv-pod.yaml'
        workspaceVolume persistentVolumeClaimWorkspaceVolume(claimName: 'workspace', readOnly: false)
    }
}
iori
  • 19
  • 1
  • This gives me this error: Invalid config option "workspaceVolume" for agent type "kubernetes". – Ben Moss Dec 02 '20 at 15:52
  • 1
    Ah, I was on an old version of the plugin that didn't support this feature yet. – Ben Moss Dec 02 '20 at 16:00
  • Is it possible to change the workspace-volume mount from /home/jenkins/agent to /var/lib/jenkins (or any other directory). I tried to manually change it in the yaml section of the jenkinsfile but it did not work ? – Syed Faraz Umar Apr 26 '22 at 08:06
1

A very simplified declarative pipeline to show just the PVC functionality:

// Uses Declarative syntax to run commands inside a container.
pipeline {
    agent {
        kubernetes {
            workspaceVolume dynamicPVC(accessModes: 'ReadWriteOnce', requestsSize: "10Gi")
            yaml '''
apiVersion: v1
kind: Pod
spec:
  securityContext:
      fsGroup: 1000
      runAsGroup: 1000
      runAsUser: 1000
  containers:
  - name: shell
    image: ubuntu
    command:
    - sleep
    args:
    - infinity
'''
            // Can also wrap individual steps:
            // container('shell') {
            //     sh 'hostname'
            // }
            defaultContainer 'shell'
        }
    }
    stages {
        stage('Main') {
            steps {
                sh 'hostname'
            }
        }
    }
}
-1

If you post your jenkins deployment then I might help in that.

Mean while you can visit this yaml that I used and worked very well for me.

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: jenkins
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      containers:
      - name: jenkins
        image: jenkins:2.32.2
        ports:
        - containerPort: 8080
        volumeMounts:
          - name: jenkins-home
            mountPath: /var/jenkins_home
      volumes:
        - name: jenkins-home
          emptyDir: {}
Dupinder Singh
  • 7,175
  • 6
  • 37
  • 61
  • 2
    You can assume Jenkins is running out of the cluster. This question is about using the kubernetes plugin to launch agents on a cluster when it's configured as a cloud. The pods are managed by Jenkins instead of any built in k8s controller. It launches the pods based on a "podTemplate" and several other configuration settings that are defined in a Jenkinsfile pipeline, so that is to say, in this scenario, there is no "jenkins deployment" if that makes sense. – Segfault May 03 '20 at 00:54