Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define workspace volume for jenkins pipeline declarative

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"
like image 321
Robert Ohajda Avatar asked Sep 04 '19 18:09

Robert Ohajda


People also ask

How do you use parameters in Jenkins declarative pipeline?

You just have to use params. [NAME] in places where you need to substitute the parameter. Here is an example of a stage that will be executed based on the condition that we get from the choice parameter. The parameter name is ENVIRONMENT , and we access it in the stage as params.

What is difference between Scriptline and declarative pipeline?

Declarative pipelines break down stages into individual stages that can contain multiple steps. Scripted pipelines use Groovy code and references to the Jenkins pipeline DSL within the stage elements without the need for steps.

What is Jnlp container in Jenkins?

The two containers share a working directory and a volume mount that defaults to /home/jenkins/agent . The jnlp container takes care of the declarative checkout source code management (SCM) action. Unless otherwise specified, all other actions are executed in the Jenkins pipeline workspace.


2 Answers

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'
            }
        }
    }
}
like image 164
hdhruna Avatar answered Sep 17 '22 11:09

hdhruna


Here is an example for declarative pipeline:

pipeline {
agent {
    kubernetes {
        yamlFile 'jenkins/pv-pod.yaml'
        workspaceVolume persistentVolumeClaimWorkspaceVolume(claimName: 'workspace', readOnly: false)
    }
}
like image 45
iori Avatar answered Sep 19 '22 11:09

iori