Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create methods in Jenkins Declarative pipeline?

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?

like image 252
vinesh vini Avatar asked Dec 04 '17 07:12

vinesh vini


People also ask

Can we use Groovy in declarative pipeline?

The tradeoff is that declarative pipelines don't allow deep integration into Groovy and Java APIs.

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.


Video Answer


4 Answers

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 /'
}
like image 111
StephenKing Avatar answered Oct 23 '22 09:10

StephenKing


You can create a groovy function like this and save it in your git which should be configured as managed library (Configure it in jenkins too):

/path/to/repo-shared-library/vars/sayHello.groovy:

Content:

def call(String name = 'human') {
    echo "Hello, ${name}."
}

You can just call this method in your pipeline using:

@Library('name-of-shared-library')_
pipeline {
    agent any
    stages {
        stage('test') {
            steps {
                sayHello 'Joe'
            }
        }
    }
}

Output:

[Pipeline] echo
Hello, Joe.

You can reuse existing functions which you keep in your library.

like image 20
lvthillo Avatar answered Oct 23 '22 09:10

lvthillo


You can also have separate groovy files with all your functions (just to keep things structured and clean), which you can load to file with pipeline:

JenkinsFile.groovy

Map modules = [:]
pipeline {
    agent any
    stages {
        stage('test') {
            steps {
                script{
                    modules.first = load "first.groovy"
                    modules.first.test1()
                    modules.first.test2()
                }
            }
        }
    }
}

first.groovy

def test1(){
    //add code for this method
}
def test2(){
    //add code for this method
}
return this
like image 21
awefsome Avatar answered Oct 23 '22 10:10

awefsome


This worked for me.It can be view with Blue Ocean GUI but when I edit using Blue Ocean GUI it removes methods "def showMavenVersion(String a)".

pipeline {
agent any
stages {
    stage('build') {
        agent any
        steps {
            script {
                showMavenVersion('mvn version')
            }
        }
    }
}

}

def showMavenVersion(String a) {
        bat 'mvn -v'
        echo a
}
like image 21
Mukesh M Avatar answered Oct 23 '22 09:10

Mukesh M