How to create methods in Jenkins Declarative pipeline?

vinesh vini picture vinesh vini · Dec 4, 2017 · Viewed 63.8k times · Source

In Jenkins scripted pipeline we are able to create methods and can call them.

Is it possible also in the Jenkins declarative pipeline? And how?

Answer

StephenKing picture StephenKing · Dec 4, 2017

Newer versions of the declarative pipelines support this, while this was not possible before (~mid 2017). You can just declare functions as you'd expect it from a groovy script:

pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                whateverFunction()
            }
        }
    }
}

void whateverFunction() {
    sh 'ls /'
}