how to declare a Pipeline global var with named parameter

542 views
Skip to first unread message

Nicolas Geraud

unread,
May 12, 2016, 5:47:41 PM5/12/16
to Jenkins Users
Hi,

I want to provide a global var on my brand new jenkins2

here is my function

// workflowLibs/vars/mavenBuild
def call(def url=null, def branch=null, def mvnArgs=["-U", "clean", "deploy"]) {

boolean defaultScm = ( url == null || branch == null )
node {

stage "Checkout"

if (defaultScm) {
checkout scm
} else {
git url: "${url}", branch: "${branch}"
}

stage "Build"

def mvnHome = tool 'MVN33'
def mvnCommamd = ["${mvnHome}/bin/mvn"] + mvnArgs
sh "${mvnCommamd.join(" ")}"
}
}


The goal of this function is to be called in a Jenkinsfile like this

mavenBuild
mavenBuild mvnArgs: ["-X", "clean", "test"]
....

first syntax works as expected, but if I call my var with a parameter, it doesn't take account and always use default params.

Is it possible to create global var with default values and named parameter ?

I know I can use this syntax : https://github.com/jenkinsci/workflow-cps-global-lib-plugin/blob/master/README.md#define-more-structured-dsl

thanks.

Nicolas Geraud

unread,
May 13, 2016, 6:22:14 PM5/13/16
to Jenkins Users
Here is how to write my function :
def call(params) {

def mvnArgs = (params == null || params.mvnArgs == null) ? ["-U", "clean", "deploy"] : params.mvnArgs
boolean defaultScm = ( params == null || params.url == null || params.branch == null )

node {

stage "Checkout"

if (defaultScm) {
checkout scm
} else {
            git url: "${params.url}", branch: "${params.branch}"
        }

stage "Build"

def mvnHome = tool 'MVN33'
        def javaHome = tool 'JDK 8'
def nodeHome = tool 'NodeJS 0.12.4'
withEnv(["PATH+MAVEN=${mvnHome}/bin",
"PATH+NODE=${nodeHome}/bin",
"JAVA_HOME=${javaHome}"]) {

def mvnCommamd = ["${mvnHome}/bin/mvn"] + mvnArgs
sh "${mvnCommamd.join(" ")}"
}
}
}


I can call it like this :

mavenBuild()
mavenBuild(branch:"master", url:"github...", mvnArgs: ["-X", "clean", "verify"]

hope this can help.
Reply all
Reply to author
Forward
0 new messages