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.
Le jeudi 12 mai 2016 23:47:41 UTC+2, Nicolas Geraud a écrit :
>
> 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.
>
--
You received this message because you are subscribed to the Google Groups
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/jenkinsci-users/15fb87b0-6375-4938-a652-95861ed5141c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.