How to check if build parameter is available in Jenkinsfile

后端 未结 5 571
Happy的楠姐
Happy的楠姐 2021-02-07 15:16

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

I tried if(getBinding().hasVariable(\"mypa

相关标签:
5条回答
  • 2021-02-07 15:34

    Newer versions make the parameters available through the params variable. If the parameter is not defined, it falls back to the configured default (see also here).

    0 讨论(0)
  • 2021-02-07 15:34

    In the latest version of jenkins println(getBinding().hasVariable("myparameter")) is permitted and does not give an error any more.

    0 讨论(0)
  • 2021-02-07 15:40

    See Getting Started with Pipeline, Build Parameters:

    Build Parameters

    If you configured your pipeline to accept parameters using the Build with Parameters option, those parameters are accessible as Groovy variables of the same name.

    UPDATE

    • This build is parameterizedAdd parameterString Parameter:

      • Name: STRING_PARAMETER
      • Default Value: STRING_PARAMETER_VALUE
    • PipelineDefinition: Pipeline scriptScript:

    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
    
    0 讨论(0)
  • 2021-02-07 15:45

    This is how I did that:

    def myParam = false
    if (params.myParam != null){
        myParam = params.myParam
    }
    

    Why do that at all when you can just define parameters in your pipeline as suggested above?

    Well, there are situations when you want your pipeline file to work regardless of if parameter is defined or not. I.e., if you're re-using your pipeline script from different Jenkins jobs and you don't want to make people define that parameter ...

    0 讨论(0)
  • 2021-02-07 15:56

    The problem: Avoiding null parameters or not configured one's in your pipeline job

    Solution: You've got 2 options in order to set default configuration to variables in declarative pipelines

    Option 1. Using jenkins ability to set parameters scope fallback , this affect your pipeline configuration on job in jenkins (if your configure things in your declarative pipeline it will override your configuration in job parameters).

    parameters{    
      booleanParam(defaultValue: false, description: 'some description', name: 'SOME_FLAG')
    }
    

    Option 2. Securing your variables yourself, check if variable is not null and then assign the variable in the pipeline , this functionality doesn't affect your job configuration and it's better in my opinion:

    some_flag = params.SOME_FLAG != null ? params.SOME_FLAG.toBoolean() : false
    
    0 讨论(0)
提交回复
热议问题