Pipeline: Check if build parameter exist

Visto 5.982 veces
Saltar al primer mensaje no leído

Sverre Moe

no leída,
29 sept 2016, 4:37:0529/9/16
a Jenkins Users
I can no longer check if a build parameter exist before accessing it

This has worked previously before I updated Jenkins and the Pipeline plugins:
def myParam = false
if (getBinding().hasVariable("MY_PARAM")) {
    myParam
= Boolean.parseBoolean(MY_PARAM)
}


final def myParam = MY_PARAM


The latter will fail on the very first build on all new branches that are added.


This will print out only for the second println.
if (getBinding().hasVariable("MY_PARAM")) {
    println
"My_PARAM1="+MY_PARAM
}
println
"MY_PARAM2="+MY_PARAM
Output: 
MY_PARAM2=false

Michael Lasevich

no leída,
29 sept 2016, 17:35:4229/9/16
a Jenkins Users
I cheat using try/catch:

try{ echo "MyParam is:"+ myParam} catch(ex) { myParam = "default" }


Its ugly, but works. Obviously you can alter this concept to whatever works for you :-)


-M

Craig Rodrigues

no leída,
29 sept 2016, 19:54:5729/9/16
a Jenkins Users
Hi,

You are right.  This has affected me too, since I use the same technique as you to figure out if 
a parameter exists.

I think that the way that build parameters are accessed inside pipeline scripts
has been changed in workfow-cps-plugin 2.17 as part of:


Can you can try something like:


node {
    envVars = env.getEnvironment()
    println envVars.containsKey("MY_PARAM")
    println enVars.containsKey("MY_PARAM2")
}


The alternative is to try accessing MY_PARAM and handle any exception thrown
if the parameter does not exist, as pointed out by Michael Lasevich.

--
Craig


--
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 jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/1902854d-b8a5-439f-8bc6-74a0f6b43762%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos