How to check if build option is available in Jenkins file

I have a script pipeline that should work with and without parameters. Therefore, I have to check if the parameter is available.

I tried if(getBinding().hasVariable("myparameter")) , but this if(getBinding().hasVariable("myparameter")) exception

 org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method groovy.lang.Binding getVariables 

Is there any other way to check if a job is set?

+5
source share
4 answers

Newer versions make parameters available through the params variable. If the parameter is not defined, it returns to the default setting (see Also here ).

+2
source

In the latest version of jenkins, println(getBinding().hasVariable("myparameter")) enabled and no longer reports an error.

+7
source

See Getting started with the pipeline, creating parameters :

Build options

If you configured the pipeline to accept parameters using the Build with parameters parameter , these parameters are available as Groovy variables with the same name.

UPDATE

  • ☑ This assembly is parameterized → Add parameter → String parameter:

    • Name: STRING_PARAMETER
    • Default value: STRING_PARAMETER_VALUE
  • Pipeline -> Definition: Pipeline script Script:

 def stringParameterExists = true def otherParameterExists = true try { println " STRING_PARAMETER=$STRING_PARAMETER" } catch (MissingPropertyException e) { stringParameterExists = false } try { println " NOT_EXISTING_PARAMETER=$NOT_EXISTING_PARAMETER" } catch (MissingPropertyException e) { otherParameterExists = false } println " stringParameterExists=$stringParameterExists" println " otherParameterExists=$otherParameterExists" 

Console output:

 [Pipeline] echo STRING_PARAMETER=STRING_PARAMETER_VALUE [Pipeline] echo stringParameterExists=true [Pipeline] echo otherParameterExists=false [Pipeline] End of Pipeline Finished: SUCCESS 
+2
source

From the documentation

If you configured the pipeline to accept parameters using the assembly with the parameters, these parameters are available as members of the params variable.

Assuming that the String parameter named Greeting is configured in the Jenkins file, he can access this parameter through $ {Params.Greeting}

+1
source

All Articles