How to check if build parameter is available in Jenkinsfile

后端 未结 5 562
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: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
    

提交回复
热议问题